Tag: chai

通过Chai发送请求

我正在试图请求我的节点JS服务器接受后/投入呼叫。 我尝试通过chai发送后调用的参数在服务器(req.body.myparam)上不可见。 我已经尝试了下面的post请求,但没有结果结束: var host = "http://localhost:3000"; var path = "/myPath"; chai.request(host).post(path).field('myparam' , 'test').end(function(error, response, body) { 和 chai.request(host).post(path).send({'myparam' : 'test'}).end(function(error, response, body) { 节点的JS代码如下: app.put ('/mypath', function(req, res){ //Handling post request to create league createDoc (req, res); }) app.post ('/mypath', function(req, res){ //Handling post request to create league createDoc (req, res); }) var createDoc […]

如何testing一个函数是否调用特定的方法/函数?

摩卡里有没有一种方法来testing一个函数是否调用特定的方法或外部函数? 我正在使用摩卡与柴,但我打开任何其他断言库。 好的,用sinon来testing一个方法是否被调用是非常容易的。 我不确定testing,看是否正在调用一个外部函数。 所以我更新了一些例子来代表一些更“真实的世界”。 我正在研究节点应用程序,所以foo.js和bar.js都是模块。 例: foo.js var bar = require('bar'); var xyz = function () {}; var Foo = module.exports = function () { this.bar(); bar(); xyz(); }; Foo.prototype.bar = function () {}; bar.js var bar = module.exports = function () {}; fooSpec.js var chai = require('chai'); var sinon = require('sinon'); var sinonChai […]

在节点的mocha js中使用全局窗口variables

我是新来的jsunit testing,我正在尝试使用mocha作为我在这个github仓库find的主干联系人pipe理器教程。 然而,我有一个全局window.ContactManagervariables,我初审想testing它是否存在,然后testing启动function后面的router.onfunction。 variables看起来像这样: window.ContactManager = { Models: {}, Collections: {}, Views: {}, start: function(data) { var contacts = new ContactManager.Collections.Contacts(data.contacts), router = new ContactManager.Router(); router.on('route:home', function() { router.navigate('contacts', { trigger: true, replace: true }); }); router.on('route:showContacts', function() { var contactsView = new ContactManager.Views.Contacts({ collection: contacts }); ….. 我的testing不起作用:var expect = require('chai')。expect; describe("Application", function() { […]

通过Chai.jstesting失败

在JUnit中,您可以通过执行以下操作来通过testing: fail("Exception not thrown"); 使用Chai.js实现相同的最好方法是什么?

安装Zombie.js错误:ReferenceError:Set未定义。 我究竟做错了什么?

背景:目前我正在阅读Ethan Brown的“ 用Node和Express进行Web开发 ”(这是一本很棒的书,对于那些学习节点和快车),而我却陷入了第5章 – 质量保险。 一切顺利。 首先我跑了以下几点: npm install –save-dev摩卡 npm安装-g摩卡 npm install –save-dev chai npm install –save-dev僵尸 问题:然后我跑(按照书的指示): mocha -u tdd -R spec qa / tests-crosspage.js 2> / dev / null 但是这没有做任何事情。 所以然后我跑了: mocha -u tdd -R spec qa / tests-crosspage.js 然后出现这个错误: /Users/esanz91/Desktop/CodingNotes/Node/MySite/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:405 var nonInheritedTags = new Set([ ReferenceError:Set没有定义 版本:只是给你们一个想法,我已经安装了以下版本。 CMD: npm list […]

如何unit testing连接到mongo的方法,而不实际连接到mongo?

我试图编写一个testing来testing一个连接到mongo的方法,但是我实际上并不想让mongo运行,并且实际上build立了连接来让我的testing成功通过。 这是我当前的testing,当我的mongo守护进程运行时,它是成功的。 describe('with a valid mongo string parameter', function() { it('should return a rejected promise', function(done) { var con = mongoFactory.getConnection('mongodb://localhost:27017'); expect(con).to.be.fulfilled; done(); }); }); mongoFactory.getConnection代码: getConnection: function getConnection(connectionString) { // do stuff here // Initialize connection once MongoClient.connect(connectionString, function(err, database) { if (err) { def.reject(err); } def.resolve(database); }); return def.promise; }

如何使空的占位符testing在摩卡故意失败?

我在NodeJS中编写一个API,并使用Mocha,Chai和SuperTest进行testing。 我正在使用一种典型的testing驱动方法来编写testing,然后使用工作代码来满足这些testing。 然而,由于所有不同排列的testing数量,我已经开始写空的占位符testing,以便我有所有的it('should…')描述来提醒我当我到达时要testing什么该function。 例如: it 'should not retrieve documents without an authorized user', (done) -> done() 这个问题是done()被调用,没有任何断言,所以testing被认为是传递,所以我已经添加了下面的断言。 false.should.equal true # force failure 但这是一个破解和摩卡显示失败的原因可能看起来很混乱,尤其是当其他完整的testing可能失败。 是否有任何官方的方式故意失败在摩卡这样的占位符testing?

Chai unittesting – expect(42).to.be.an('integer')

根据http://chaijs.com/api/bdd/#a ,可以使用a / an来检查variables的types。 。一种) @param{ String } type @param{ String } message _optional_ a和an断言是可以用作语言链或声明一个值types的别名。 但是,我无法检查variablesbeeing整数。 给定的例子,例如expect('1337').to.be.a('string'); 为我工作,但以下不: expect(42).to.be.an('integer'); expect(42).to.be.an('Integer'); expect(42).to.be.an('int'); expect(42).to.be.an('Int'); 运行摩卡时,他们都给我以下错误: Uncaught AssertionError: expected 42 to be an integer 我如何用chai来testing一个整数的variables?

如何unit testingexpress路由器路由

我是Node和Express的新手,我试图unit testing我的路线/控制器。 我已经从我的控制器中分离出我的路线。 我如何去testing我的路线? configuration/ express.js var app = express(); // middleware, etc var router = require('../app/router')(app); 应用程序/路由器/ index.js module.exports = function(app) { app.use('/api/books', require('./routes/books')); }; 应用程序/路由器/路由/ books.js var controller = require('../../api/controllers/books'); var express = require('express'); var router = express.Router(); router.get('/', controller.index); module.exports = router; 应用程序/ API /控制器/ books.js // this is just an example […]

如何testing节点中的事件发射器

可以说我想写这个简单的任务。 但我想写一个testingvalidation: 这个任务发射物体。 对象有一个属性名称。 我正在testing摩卡和柴期望。 提前致谢。 我已经尝试了所有可能的变体,但不能提出解决scheme。 var util = require('util'), EventEmitter = require('events').EventEmitter; function SomeTask() { var self = this; setInterval(function() { self.emit('data', { name: 'name' }); }, 5000); } util.inherits(SomeTask, EventEmitter); module.exports = SomeTask;