Tag: should.js

用Mongoosetestingasynchronous中间件function

我正在Mongoose中使用一个save中间件来在数据库中创build一个活动日志,只要采取一些行动。 就像是 UserSchema.post("save", function (doc) { mongoose.model("Activity").create({activity: "User created: " + doc._id}); }); 这似乎工作正常,但问题是,我不能testing它,因为没有办法传递一个callback post (这可能没有意义)。 我用mochatesting了这一点: User.create({name: "foo"}, function (err, user) { Activity.find().exec(function (err, act) { act[0].activity.should.match(new RegExp(user._id)); done(); }); }); 问题是,调用.find之前, Activity.create显然没有完成。 我可以通过在setTimeout包装.find来解决这个问题,但是这对我来说似乎很.find 。 有什么方法可以testingasynchronousmongoose中间件操作?

在asynchronous摩卡testing中超时错误

我正在使用摩卡和应该testing简单的数据库查询,我想运行简单的Moongose模式函数的asynchronoustesting,但我每次都超时错误。 Error: timeout of 15000ms exceeded at null.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:165:14) at Timer.listOnTimeout [as ontimeout] (timers.js:110:15) 我甚至使用this.timeout(15000) ,也尝试了超时15000在摩卡命令,但没有成功,什么超时时间我给我得到这个错误。 只有同步testing通过。 以下是我想testing的testing和function。 我的摩卡testing: – describe('#getFacility()', function () { this.timeout(15000); it('should get facility successfully', function (done) { db.getFacilites(Data.testFacility, function (err, facilities) { if (err) throw err; facilities.Name.should.equal(Data.testFacility.body.Name); done(); }) }); }); 我的资料: – testFacility : { params: { clientId:"51c85c3267b6fc1553000001" } […]

与Coffeescript一起使用should.js

我想在我在Coffeescript中编写的Node.js项目中使用should.js和mocha。 在纯Javascript中,should.js中的expression式就像 (function(){ throw new Error('fail'); }).should.throw(); 现在我想在Coffeescript中编写相同的expression式。 就像是 object.function().should.throw 但是,编译 object["function"]().should["throw"]; 我的Coffescript代码中的错误在哪里?

Should.js:检查两个数组是否包含相同的string

我有两个数组: var a = ['a', 'as', 'sa']; var b = ['sa', 'a', 'as']; 应该有什么特别的东西来testing这两个数组是否有相同的项目? 任何喜欢 should(a).be.xyz(b) 那可以testing他们? 在这里,xyz是我正在寻找的。

Should.js在一个属性上链接多个断言

我有这样一个对象: var obj = { "uuid": "60afc3fa-920d-11e5-bd17-b9db323e7d51", "type": "candy" } 我想写一个testing,首先检查对象的属性“uuid”,然后“uuid”是一个特定的长度(36个字符)。 试试这个不行 obj.should.have.property('uuid').which.should.have.length(36) 它失败: Uncaught AssertionError: expected Assertion { obj: '60afc3fa-920d-11e5-bd17-b9db323e7d51', params: { operator: 'to have property \'uuid\'' }, negate: false } to have property 'length' of 36 (got [Function]) 而这(实际上不会使语法意义 – 因为它将适用于父对象而不是值) obj.should.have.property('uuid').and.be.length(36) 哪个失败: Uncaught TypeError: usergridResponse.entity.should.have.property(…).which.should.be.equal.to is not a function 即使这不起作用: obj.should.have.property('uuid').which.equals('60afc3fa-920d-11e5-bd17-b9db323e7d51') 那么链接对象属性的断言的正确方法是什么?

Node.js + Chai / Mocha /应该:针对相同的响应运行多个testing

我正在使用与以下类似的东西来执行一系列使用Mocha的APItesting。 这很好,但是它需要为每个testing制作一个单独的API调用。 我想要使​​用相同的API调用,并针对该响应运行多个testing。 我一直在阅读你before可以使用的方法,但是Web上的所有例子都没有显示它使用API​​调用? var chai = require('chai'); var request = require('request'); var async = require('async'); var assert = chai.assert, expect = chai.expect, should = chai.should(); describe('/', function () { it('should return 200', function (done) { request.get('http://localhost:8000', function (err, res, body) { res.should.have.status(200); done(); }); }); it('should say "Hello, world!"', function (done) { request.get('http://localhost:8000', function […]

res.should.have.status给我错误

我是新来的摩卡和should.js。 我试图检查响应的状态,但它给了我TypeError: Object #<Assertion> has no method 'status'代码是这样的: describe('Local signup', function() { it('should return error trying to save duplicate username', function(done) { var profile = { email: 'abcd@abcd.com', password: 'Testing1234', confirmPassword: 'Testing1234', firstName: 'Abc', lastName: 'Defg' }; request(url) .post('/user/signup') .send(profile) .end(function(err, res) { if (err) { throw err; } res.should.have.status(400); done(); }); }); 我也注意到,虽然我已经声明var should […]

找不到模块'./lib/should'

我有一个testing代码,并试图运行它 var should = require("should") describe('Array', function(){ describe('#indexOf()', function(){ it('should return -1 when the value is not present',function(){ [1,2,3].indexOf(5).should.equal(-1); [1,2,3].indexOf(0).should.equal(-1); }) }) }) 当我运行它我得到错误 Error: Cannot find module './lib/should' at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require (module.js:364:17) at require (module.js:380:17) at Object.<anonymous> (/home/ritesh/projects/passport-topcoder/node_modules/should/index.js:2:18) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load […]

断言具有相同内容的文件

我正在使用摩卡/ supertest / should.js来testing我的rest服务 GET /files/<hash>将文件作为stream返回。 如何在should.js中声明文件内容是一样的? it('should return file as stream', function (done) { var writeStream = fs.createWriteStream('test/fixtures/tmp.json'); var req = api.get('/files/676dfg1430af3595'); req.on('end', function(){ var tmpBuf = fs.readFileSync('test/fixtures/tmp.json'); var testBuf = fs.readFileSync('test/fixtures/test.json'); // How to assert with should.js file contents are the same (tmpBuf == testBuf ) // … done(); }); });

使用Mocha&Should.js组织testing

我是使用Mocha&should.js进行unit testing的新手。 我正在使用Mocha的BDD来testing我的应用程序。 我正在testing的应用程序有不同的组件,如帐户,产品和订单。 在将代码移动到git仓库之前,我想testing应用程序的所有方面。 我对所有组件都有不同的testing文件。 例如,账户的account.js,订单的order.js等等。 我想testing一个临时testing帐户的所有组件。 所以stream程是: 创buildtesting帐户 testing与帐户相关的所有function(更新configuration文件,更改密码等) testing帐户产品的所有function testing账户订单的所有function 删除testing帐户及与其相关的所有信息 我的问题是如何确保在执行其他testing之前创build临时帐户? 因为我在不同的文件中testing用例,所以我如何确保它们的执行顺序与上面提到的相同? 还有其他更好的方法来testing应用程序吗? 谢谢。