Tag: 摩卡

摩卡testingnodejs mssql – 连接已closures

尝试使用Mocha为使用MsSql的NodeJS设置testing。 但每次我运行我的testing,我得到一个“连接已closures”。 奇怪的是,当我自己运行应用程序时,它确实工作,然后,我从数据库中获取数据,并且连接没有太快closures。 我的testing代码: var express = require('express'); var expect = require('chai').expect; var calendar = require('./../Server/calendarDatabase'); describe("Calendar", function () { describe("Database", function () { it("should get stuff from the database", function (done) { calendar.getAll().then(function (returnValue) { console.dir(returnValue); expect(returnValue.count).to.equal(5); done(); }); }); }); }); 我的日历数据库代码看起来像 var express = require('express'); var sql = require('mssql'); var config = […]

对没有调用callback的NodeJS函数进行摩卡testing

我有一个function,使用Facebook页面发送消息给Facebook用户。 // send text message to an user function sendTextMessage(sender, text, callback) { messageData = { text: text } request({ url: 'https://graph.facebook.com/v2.6/me/messages', qs: { access_token: token }, method: 'POST', json: { recipient: { id: sender }, message: messageData, } }, function (error, response, body) { if (error) { console.log('Error sending message: ', error); callback(-1); } […]

Babel / Mocha:摩卡在全球安装,但describe()没有定义

我试图运行一个testing,我得到这个错误: /dev/tests/counters.spec.js:12 describe('Work damn you!', function () { ^ ReferenceError: describe is not defined 摩卡在全球和本地安装到项目中。 我不确定是否正确,但是这是我的testing脚本: "test": "mocha –compilers js:babel-register –require dev/tests/counters.spec.js" 我会去做任何明显错误的事情吗? 任何帮助将受到欢迎。

运行iron-node时,module.js找不到模块

我正在尝试使用iron-node(v2.2.17)来debugging我的摩卡unit testing。 从我的package.json运行这个命令时,unit testing运行正常: "test": "cross-env NODE_ENV=test mocha test/.setup.js –reporter progress –compilers js:babel-core/register –require babel-polyfill –recursive \"./src/**/*.spec.js\" \"./src/**/*.integrationSpec.js\" \"./test/**/*.spec.js\"", 但是,当我运行这个命令时,testing失败: "debug:test": "cross-env NODE_ENV=test iron-node node_modules\\mocha\\bin\\_mocha test/.setup.js –reporter progress –compilers js:babel-core/register –require babel-polyfill –recursive \"./src/**/*.spec.js\" \"./src/**/*.integrationSpec.js\" \"./test/**/*.spec.js\"", 控制台中的错误是: Error: Cannot find module 'src/framework/api/entityAddresses/entityAddressesAc tions' – module.js:16 require internal/module.js:16:19 – entityAddressesActions.spec.js:5 Object.<anonymous> entityAddressesActions.spec.js:5:1 该模块位于C:\TFS\Dev-UI\WebApp\Src\Web\Web\src\framework\api\entityAddresses\entityAddressesActions.js 我的项目文件夹是C:\TFS\Dev-UI\WebApp\Src\Web\Web ,我的process.env.NODE_PATH也是C:\TFS\Dev-UI\WebApp\Src\Web\Web 。 […]

柴没有达到.end()

我正在使用摩卡和柴来testing我的节点/快递API,我不明白为什么testing没有达到.end() 这是testing: it('should authenticate successfully with user credentials', function (done) { agent .post('/login') .set('Content-Type', 'application/x-www-form-urlencoded') .send({ 'username': 'username', 'password': 'password'}) .end(function (err, res) { console.log(res); console.log('***************************Authenticated*********************************************'); expect(res).to.have.status(200); }); done(); }); 这里是我打的路线: app.post('/login', passport.authenticate('ldapauth', { successRedirect: '/' })); 我想我的问题可能是事实上没有正式的回应,而是一个redirect,但我不知道如何处理它。

aws-sdk上传到S3在KOA中工作,但在通过Mocha调用时停止工作

我需要从koa上传一个文件到s3,我对koa来说很新,可能丢失了一些明显的东西。 它实际上完成了200,但文件从来没有出现在S3上。 这里是我的app.js的一个片段: 'use strict'; var jwt = require('koa-jwt'); var bodyParser = require('koa-bodyparser'); var koaBody = require('koa-body'); const app = module.exports = require('koa')() .use(koaBody({multipart:true})) // this is to pase only multipart forms .use(require('./routes/common')) .use(require('./routes/auth')) .use(require('./routes/users_public')) .use(jwt({ secret: SECRET })) //protected routes below this line .use(require('./routes/subcontractors_private')) .listen(process.env.PORT || 3000); subcontractors_private.js看起来像这样: 'use strict'; var AWS = require('aws-sdk'); […]

sinon没有正确恢复stubbed原型的方法?

看起来,sinon可能不会正确地恢复残桩原型。 在我作为一个bug报告之前,有人可以告诉我,如果我做错了什么? 这失败了: 下面的代码似乎正确存根net.Socket.prototype.connect,但不正确地恢复它:后续testing – 与此代码无关,但依赖net.Socket – 开始失败: it('passes host and port to the net.Socket().connect()', sinon.test(function() { var stub = sinon.stub(net.Socket.prototype, 'connect'); var host = '11.22.33.44'; var port = 1234; var il = new InstrumentLink(host, port); expect(stub).to.have.been.calledWith(host, port); })); 请注意,我正在使用“包装函数” sinon.test(function() …)来创build和还原沙箱。 这工作: 另一方面,下面的代码正确地恢复了存根,我的testing套件的其余部分继续运行: var stub; beforeEach(function() { stub = sinon.stub(net.Socket.prototype, 'connect'); }); afterEach(function() { […]

Node.js sinon在并行执行中桩存函数导致失败的testing

我有两个testing用例testing相同的function只是采取两个不同的执行path,所以说明: MyClass.prototype.functionBeingTested = function() { if (this.check1()) { this.isCheck1Called = true; } else if (this.check2()) { this.isCheck1Called = false; } else { … } }; 我的2个testing用例如下: it('should take check1() execution path', function() { var myClass= new MyClass({}, {}, {}); var check1Stub sinon.stub(MyClass.prototype, 'check1'); check1Stub.returns(true); myClass.functionBeingTested(); myClass.isCheck1Called.should.equal(true); }); it('should take check2() execution path', function() { var […]

使用Mocha.js时,node.js套接字挂起

我正在使用节点v6.2.0并通过Mocha&Chai进行testing。 我写了一个API,当我用邮递员/网站/节点CLItesting它,它的工作很好,但是当我使用摩卡来testing它,我得到一个错误,说: {[错误:连接ECONNREFUSED 127.0.0.1:4001]代码:'ECONNREFUSED',errno:'ECONNREFUSED',系统调用:'连接',地址:'127.0.0.1',端口:4001} 现在,事情是,在http://localhost:4001testing连接的先前testing工作正常… 这是testing代码 – describe('/api/getAlbums', function () { this.timeout(5000); it('should get an array of 4 objects', function (done) { http.get('http://localhost:4001/api/getAlbums?uid=some_uid', function (res) { console.log(`Got response: ${res.statusCode}`); done(); }).on('error', (e) => { console.log(`Got error: ${e.message}`); console.log('e', e); done(); }); }); }); 再次,当我在CLI中运行相同的function(没有done(); ),它工作正常。 我去了很多文件,但找不到任何东西,很想得到你的帮助,谢谢。

用Mocha和Typescript模拟依赖关系

我有一个使用摩卡的打字稿项目。 假设我们有两个模块,如下所示。 // http.ts export class Http { } // app.ts import Http from './http'; export class App { } 我在testing应用程序时如何模拟Http模块? testing通过npm脚本执行如下。 "test": "cross-env NODE_ENV=test ./node_modules/mocha/bin/mocha", 摩卡选项(mocha.opts)如下所示。 test/setup.ts –compilers ts:ts-node/register –compilers tsx:ts-node/register ./src/**/*.spec.ts