Tag: 摩卡

用MochatestingExpress和Mongoose

我正在尝试使用Mocha和Chai来testing我的REST API端点处理程序,该应用程序是使用Express和Mongoose构build的。 我的处理程序主要是这样的forms: var handler = function (req, res, next) { // Process the request, prepare the variables // Call a Mongoose function Model.operation({'search': 'items'}, function(err, results) { // Process the results, send call next(err) if necessary // Return the object or objects return res.send(results) } } 例如: auth.getUser = function (req, res, next) { […]

允许chai / mochatesting将错误冒泡到process.on

我正在编写一个捕获顶级未捕获错误的节点模块,并希望为其编写一些testing。 不幸的是,我最喜欢的框架似乎有意识地抛出和捕获未捕获的exception的问题。 如果我抛出exception,那么错误,然后testing失败。 如果我抛出并捕获错误,它不会冒泡process.on('uncaughtException') 代码atm不起作用 it('Catches errors and return the user and line number', function(done) { blame.init(function (res) { console.log('\n\n', res, '\n\n'); expect(true).should.equal(true); done(); }); expect(function () { undefinedFunction(); }).to.throw('undefinedFunction is not defined'); });

Webdriver(wd)testing在本地工作,但超时Travis CI?

我有一些testing用gulp-mocha-selenium写在引擎盖下使用wd。 注意这是“wd”驱动程序而不是vanilla Webdriver可能很重要。 testing在这里: https : //github.com/UWFosterIT/react-starter/blob/master/test/acceptance/hello-spec.js 我的travis.yml是: language: node_js node_js: – "0.10.37" before_install: – "export DISPLAY=:99.0" – "sh -e /etc/init.d/xvfb start" – "npm install -g eslint gulp eslint-plugin-react webpack" before_script: – "sleep 30" script: – "gulp check" – "gulp test:server" – "gulp test:acceptance" addons: firefox: "31" testing在当地正常工作,但在TravisCI.org上超时。 我不知道从哪里开始寻找。 特拉维斯的结果: https : //travis-ci.org/UWFosterIT/react-starter/builds/55222925

使用mocha.js在node.js中testing外部API调用

我正在尝试为我的npm模块编写testing,该模块负责与后端api进行通信。 这个模块将坐在一个cordova的Android应用程序,并会照顾任何API调用。 我所遇到的问题似乎是对摩卡的理解,但是我已经在互联网上看了一遍,也找不到解决办法,所以我转向群众。 作为一个例子,我有一个像这样的function test: function() { request.get({ url: defaultHost, headers: { } }, function(err, httpResponse, body) { if(err) { return err; } else { console.log(body); return body; } }); } 这个作品会的。 我现在正在尝试在摩卡中为它创buildtesting。 我得到的问题是,我不知道如何从.get调用返回到摩卡testing函数。 api返回json,所以我知道我将不得不做一个平等的比较,但是目前我甚至不能打印结果。 我认为问题在于,用我的其他摩卡testing,我可以工作,他们都有一个论点,你通过的地方,因为这不是。 我目前的testing代码是这样的。 describe('#test', function() { it('tests api reachability;', function() { var test = new test(); }); }); 如果有人可以解释什么是必要的,甚至只是指向我在谷歌的正确方向,这将是很棒的。 我通常在谷歌相当好,但没有find方向在这一个。

Mongoose不会在Mocha中正确删除数据库和closures连接

我有两个简单的testing,但其中一个testing通过,但不是另一个,因为架构再次编译。 OverwriteModelError:编译后无法覆盖CheckStaging模型。 这是我的一个testing,因为它正在运行第一。 var mongoose = require('mongoose'), StagingManager = require('../lib/staging_manager'), expect = require('expect.js'); describe('Staging manager', function() { var StagingModel; beforeEach(function(done) { mongoose.connect('mongodb://localhost/BTest'); StagingModel = new StagingManager(mongoose).getStaging(); done(); }); describe('find one', function() { it('should insert to database', function(done) { // Do some test which works fine }); }); afterEach(function (done) { mongoose.connection.db.dropDatabase(function () { mongoose.connection.close(function () […]

我如何使用Mocha和伊斯坦布尔编写出色的testing用例?

我正在尝试使用chai和Mocha为Mongoose模型函数编写unit testing用例。 模型function function getDetails(parameter, fn) { Model.findOne({ parameter: parameter }) .lean() .exec(function(err, document) { if (err) { return fn(err, null); } return fn(err, document); }); }; unit testing用例 describe('→ Database model functions.', function() { it('getDetails() – should fetch and return details from database', function(done) { model.getDetails(parameter, function(err, document) { expect(err).to.be.null; expect(document).not.to.be.null; expect(document).to.be.an('object'); done(); }); […]

node.js在test中设置process.envvariables

当我用mocha在node.js中运行testing时,如何设置时态环境variables? 在一个模块中,我有一个variables,取决于一个环境variables var myVariable = proccess.env.ENV_VAR; 现在我使用rewire模块, var rewire = require('rewire'); var myModule = rewire('../myModule'); myModule.__set__('myVariable', 'someValue'); 存在一个更简单的方法? 没有rewire模块?

如何将对象传递给Mochatesting

我有一个自定义的节点服务器,并将其作为对象传递给一些testing。 这是我的Gruntfile: module.exports = function(grunt) { // Add the grunt-mocha-test tasks. grunt.loadTasks('node_modules/grunt-mocha-test/tasks'); grunt.initConfig({ // Configure a mochaTest task pkg: grunt.file.readJSON('package.json'), mochaTest: { test: { options: { reporter: 'spec', globals : 'MyServer' }, src: ['./server/test/custom/*.js'] } } }); 如何在我的testing中使用我在Gruntfile中创build的variables? 还有另一种方式来传递东西到我的testing?

Babel不与Mocha一起工作并启动Node服务器

我正在帆和摩卡上运行sailsjs,mocha和babel。 当我运行之前,我的前function启动帆应用程序运行testing之前,我得到这个: > PORT=9999 NODE_ENV=test mocha –recursive –compilers js:babel/register lifting sails 1) "before all" hook 0 passing (757ms) 1 failing 1) "before all" hook: Uncaught Error: only one instance of babel/polyfill is allowed 对于我的生活,我无法弄清楚如何让摩卡运行巴贝尔同时运行巴贝尔的工作。 我的before()代码如下所示: import Sails from 'sails' // Global before hook before(function (done) { console.log('lifting sails') // Lift Sails with test database Sails.lift({ […]

与摩卡失败的快速testing

我有一个Express服务器,我试图用SuperTesttesting。 下面的testing没有通过,我不知道为什么。 我看到响应的状态是200(在res.status.should.equal(200)放置了断点)。 为什么这个testing仍然被标记为我的摩卡失败? it('Should test invoke user method', function (done) { supertest(app) .post('/save/test1/test2/test3') .expect(200) .end(function (err, res) { res.status.should.equal(200); done(); }); });