Tag: 摩卡

摩卡testing:未捕获TypeError:无法读取null的属性“状态”

学习TDD和我的“Hello World”服务器响应的第一个简单testing在Mocha中是失败的。 我正在使用Mocha.js,Superagent和Expect.js。 当我curl -i localhost:8080 ,我得到正确的响应和状态代码。 HTTP/1.1 200 OK Content-Type: text/plain Date: Mon, 27 Apr 2015 17:55:36 GMT Connection: keep-alive Transfer-Encoding: chunked Hello World testing代码: var request = require('superagent'); var expect = require('expect.js'); // Test structure describe('Suite one', function(){ it("should get a response that contains World",function(done){ request.get('localhost:8080').end(function(res){ // TODO check that response is okay […]

done()callback的要点是什么?

在Mochajs中,他们使用“done()”来testingasynchronous代码,如下所示: describe('User', function() { describe('#save()', function() { it('should save without error', function(done) { var user = new User('Luna'); user.save(function(err) { if (err) throw err; done(); }); }); }); }); 这是什么意思? 我做了console.log(done.toString()),我得到这个: function (err) { if (err instanceof Error || toString.call(err) === '[object Error]') { return done(err); } if (err) { if (Object.prototype.toString.call(err) === '[object Object]') […]

表示中间件testing摩卡柴

有没有办法用express来testing这些中间件: module.exports = function logMatchingUrls(pattern) { return function (req, res, next) { if (pattern.test(req.url)) { console.log('request url', req.url); req.didSomething = true; } next(); } } 我发现的唯一中间件testing是: module.exports = function(request, response, next) { /* * Do something to REQUEST or RESPONSE **/ if (!request.didSomething) { console.log("dsdsd"); request.didSomething = true; next(); } else { // Something went […]

如果在抛出任何错误的时候如何在promise中做出断言,不会冒出来呢?

与摩卡运行这个结果超时,而不是让摩卡赶上错误,所以它可能会立即失败.. var when = require('when'); var should = require('should'); describe('', function() { it('', function(done) { var d = when.defer(); d.resolve(); d.promise.then(function() { true.should.be.false; false.should.be.true; throw new Error('Promise'); done(); }); }); }); http://runnable.com/me/U7VmuQurokZCvomD 是否有另外一种方式在诺言中作出断言,如果失败,他们会被摩卡抓住,导致立即失败? 根据柴的build议,我研究了它,似乎我必须有一个直接访问的承诺对象,对不对? 问题是,我没有直接使用诺言。如果我简化,我的坏,但这将是一个更接近现实的例子 function core_library_function(callback){ do_something_async(function which_returns_a(promise){ promise.then(function(){ callback(thing); }); }); } describe('', function() { it('', function(done) { core_library_function(function(thing){ … done(); }); }); […]

如何检测是否在node.js中运行摩卡testing?

我想确保在代码以testing模式运行的情况下,它不会(意外)访问错误的数据库。 检测代码当前是否在testing模式下运行的最佳方法是什么?

使用cloud9的摩卡testing,从node.js执行摩卡testing

我想知道是否有办法从node.js以编程方式执行mochatesting,以便我可以将unit testing与Cloud 9集成在一起。云9 IDE具有很好的function,只要保存javascript文件,它就会查找带有相同的名称,以“_test”或“Test”结尾,并使用node.js自动运行。 例如,它有一个自动运行的文件demo_test.js中的代码片段。 if (typeof module !== "undefined" && module === require.main) { require("asyncjs").test.testcase(module.exports).exec() } 有没有这样的事情,我可以用来运行摩卡testing? 像摩卡(this).run()?

如何从mocha.opts文件正确地要求模块

我用我的摩卡单位testing使用expect.js库。 目前,我需要在每个文件的第一行上的库,如下所示: var expect = require('expect.js'); describe('something', function () { it('should pass', function () { expect(true).to.be(true); // works }); }); 如果可能的话,我想从每个文件的第一行中删除样板需求代码 ,并让我的unit testing神奇地知道expect 。 我想我可以使用mocha.opts文件来做到这一点: –require ./node_modules/expect.js/index.js 但是现在我运行我的testing时出现以下错误: ReferenceError:expect没有定义 这似乎是有道理的 – 它如何知道在我的testing中expect的引用指的是expect.js库导出的内容? 期望的库肯定会被加载,就像我改变path到不存在的东西那么摩卡说: “错误:找不到模块”./does-not-exist.js“” 有什么办法可以完成我想要的吗? 如果可能有帮助的话,我正在进行一些testing。

用Mochatesting访问内部描述块时,外部描述块中的variables是不确定的

我有一个testing套件,如下所示: (请注意,顶部的accountToPostvariables(在第一个describe块下面) describe('Register Account', function () { var accountToPost; beforeEach(function (done) { accountToPost = { name: 'John', email: 'email@example.com', password: 'password123' }; done(); }); describe('POST /account/register', function(){ describe('when password_confirm is different to password', function(){ //accountToPost is undefined! accountToPost.password_confirm = 'something'; it('returns error', function (done) { //do stuff & assert }); }); }); }); 我的问题是,当我尝试修改我的嵌套的描述块中的accountToPost ,它是未定义的… […]

使用摩卡和supertest不能testingDELETE方法

我正在尝试为节点应用程序构build一个RESTful API。 我build立了路线,一切运行良好。 但是,当我尝试testing它,它不能让DELETE方法工作,尽pipe它通常不在testing中正常工作。 这里是服务器和testing的代码。 服务器: // set up var express = require('express'); var app = express(); // create our app w/ express var path = __dirname; //root path // configuration app.configure(function() { app.use(express.static(path)); //app.use(express.logger('dev')); // log every request to the console app.use(express.json()); app.use(express.urlencoded()); // pull information from html in POST app.use(express.methodOverride()); // simulate DELETE […]

eventEmitter侦听器和具有不同参数的发射器

我们可以有一个发射器的多个监听器,每个工作在不同数量的参数上? 让事件发射器是这样的: evetE.emit('pre', global, file, self); corresponding event listeners: //Listener 1 m.eventE.on('pre', function() { //TODO }) //Listener 2 eventE.on('pre', function(context, file, m){ console.log(context.ans); }); //Listener 3 eventE.on('pre', function(context){ console.log(context.ans); }); //Listener 4 this.eventE.on('pre',function (context) {}) 如果以上是真的,那么哪个参数传给哪个监听器呢?