Tag: should.js

使用非可枚举属性扩展对象

我正在阅读should.js的readme ,并获得了“OMG IT EXTENDS OBJECT ???!?!@”部分的内容 是的,是的,它只是一个单一的getter,不会破坏你的代码,因为它用一个非枚举属性正确地做了这个。 我一直听说你不应该扩展object原型,但从来没有真正明白为什么。 有人能解释为什么一般来说,扩展object是不好的(它可以打破什么)? 还解释什么是不可枚举的属性,以及如何帮助?

使用Mocha和should.js进行NodeJStesting数据库

我用摩卡testing我的NodeJS应用程序,应该。 第一次testing顺利进行时,第二次失败(错误等于空)。 在这两个testing中,如果在callback中有一个有效的用户(两者在mongoose中都有相同的ID)。 testing显然不等待数据库操作发生。 describe("User", function(){ before(function (done) { // clear database model.UserModel.collection.remove(done); }) it("should save", function(done){ user1.save(function(error, user){ should.not.exist(error); user.should.have.property("first_name", "Rainer"); done(); }) }) it("should not save duplicate user", function(done){ user1.save(function(error, user){ should.exist(error); done(); }) }) }) 当我在第一个testing的callback中放置第二个testing时,它也不起作用。 我想testing一个重复的键错误,但不能达到给定的情况。

Should.js – 确定多个对象中的单个字段与单个对象之间的等式

我正在用美妙的Mocha和Should.js来testingapi。 我做一个GET并接收一个对象数组,例如: [{ username: 'boris', something: 123 }, { username: 'jeremy', something: 456 }, { username: 'steven', something: 789 }, { … }, { … }, { … }] 对于每个对象,我想确保用户名值匹配不同对象中的属性: mockUsernames = { a : 'bill', b : 'ben', c : 'boris' }; 你怎么能做到这一点? 作为一个例子,我想要这样的东西: .get() … var someData = res.body; someData.forEach(function (e){ e.username.should.equal(mockUsernames.a || […]

摩卡正确运行,但显示未定义

我使用mocha来testing我的nodejs代码,testing运行正常,但是当我使用mocha test.js来运行testing时,似乎我传递给describe的string不显示。 代码如下: var assert = require("should"); describe("FrontEndTest", function(){ describe('websocket establish connection', function(){ it('should establish connection correctly', function(done){ var res; var wsClient = create_ws_client('ws://127.0.0.1:9876','brain_burst'); wsClient.on('connect', function(connection){ res = true; res.should.be.true; done(); }); wsClient.on('connectFailed', function(error){ res = false; res.should.be.true; done(); }); console.log(res); }); it('should disconnected by server.(also, that may crash server if there is not a […]

testingexpress API需要本地服务器

我读过我可以在一个快速应用程序(nodeJS)与超级testing运行摩卡testing,因此它不需要在不同的terminal会话中运行应用程序。 无论我尝试它总是以连接错误结束。 为了configuration我们的持续集成,显然集成和unit testing(mocha,supertest,should)应该能够运行,而节点服务器也不运行 书面testing是validation我们的应用程序的内部API端点谁可以解释如何运行testing,而无需运行应用程序的快递服务器,以便他们可以与例如strider

ChaiJS应该 – testing空string

根据shouldJS网站上的文档,我应该可以做到这一点: ''.should.be.empty(); ChaiJS网站没有应用语法的例子,因为它们会列出所期望的 ,看起来像上面的那样。 然而这产生: TypeError: Property 'empty' of object #<Assertion> is not a function 什么是与应该风格的柴empty()语法empty() ?

检查should.js和Mocha抛出的新错误

我有这样的错误: if (somethingBadHappened) { throw new Error('what were you thinking, batman?') } 现在我想写一个testing来检查这个在预期的时候抛出: should(MyClass.methodThatShouldThrow()).throws('what were you thinking, batman?') 但是这实际上会引发错误。 我究竟做错了什么? Should.js文档非常轻,因为它从assert.throwsinheritance,但是即使是文档也不能以'should.js'的方式工作?

如何扩展'应该'图书馆

我想介绍应该断言图书馆,工作testing我的node.js应用程序,我的附加function。 就像这个伪代码一样 should = require "should" myExists = (obj, msg) -> # my special exist logic containSomething = (obj, msg) -> # my special assert logic should.myExists = myExists should.containSomething = containSomething describe.only "`my extra `", -> it 'should be working', (done) -> obj = {} obj.should.myExists obj.should.not.myExists obj.should.containSomething {cool:obj} obj.should.not.containSomething {cool:obj} done() 任何build议如何在实践中做到这一点?

从testing中解耦查询的最佳实践

我正在寻找使用mocha或sinontestingmysql查询的最佳实践。 我试图testing非常基本的数据库查询,如保存: User.prototype.save = function(cb) { var query = 'INSERT INTO user (name, email, password, img, description)' + 'values (?, ?, ?, ?, ?)'; var params = [this.name, this.email, this.pass, this.img, this.description]; db.query(query, params, function(err, rows) { if (err) return cb(err); cb(rows.insertId); } ); }; 我相信我想做一个模拟数据库,并使用该对象,而不是我的数据库对象,但我也想分开testing逻辑从应用程序逻辑。 我尝试了一些变化 describe('User', function() { before(function() { mock = sinon.mock(require('../middleware/db')); […]

testing应该通过stream错误,失败的成功

我试图写一个摩卡testing传递stream错误,但如果stream没有错误结束失败。 检测错误是没有问题的,但总是调用完成处理程序,即使stream被强制错误。 在这段代码中, error处理程序的should.exist(err)断言可以正常工作,但是finish处理程序仍会抛出错误。 describe('catch stream errors', function() { it('should throw an error', function(done) { var stream = failStream(); stream.on('error', function(err) { should.exist(err); done(); }) stream.on('finish', function() { done(new Error('Why does this still run?')); }); stream.write(); stream.end(); }) }) 一种解决方法,看起来有点像黑客攻击,就是把一个erroredvariables放在处理程序的上面,然后把它翻到error处理程序中,并检查finish处理程序中的值。 似乎应该有一个更好的方式来处理这个问题。 var errored = false; stream.on('error', function(err) { should.exist(err); errored = true; done(); }) stream.on('finish', […]