Tag: unit testing

Sinonfunction存根:如何在模块内调用“自带”function

我正在为node.js代码编写一些unit testing,并使用Sinon通过存根函数调用 var myFunction = sinon.stub(nodeModule, 'myFunction'); myFunction.returns('mock answer'); nodeModule看起来像这样 module.exports = { myFunction: myFunction, anotherF: anotherF } function myFunction() { } function anotherF() { myFunction(); } Mocking对于像nodeModule.myFunction()这样的用例显然nodeModule.myFunction() ,但是我想知道如何在用nodeModule.anotherF()调用时如何在另一个F()内模拟myFunction()调用?

在Nodejs中实现unit testing – 意外的失败

无法执行unit testing 问题是testing没有通过,尽pipe当我执行testing时请求和响应数据被添加到数据库。 请提出一些方法来通过这个testing。 预期的状态码200没有在这个testing中发生。 集成testing正在对我的应用程序工作,但我无法debugging此unit testing错误。 //testfile var db = require('../mongodb'), mongoose = require('mongoose'), posts = require('../api/addTodo'), should = require('should'), testUtils = require('./utils'); describe("Add Api", function () { var dummyPost, id; before(function (done) { mongoose.connect('mongodb://localhost:27017/todosdb_test', function() { console.log('Connected To:'+'mongodb://localhost:27017/todosdb_test'); done(); }); dummyPost = new db.Todos({ 'admin': 'Dumm_admin', 'text': 'Dummy', 'completed': true }); dummyPost.save(function (err, […]

节点JStesting – 模拟file upload

我正在使用node-mocks-http来testing我的API,并且在模拟file upload时遇到了问题。 这是我的尝试: var response = buildResponse() var request = http_mocks.createRequest({ method: 'POST', url : '/test', headers: { 'Content-Type' : 'multipart/form-data;', 'Transfer-Encoding': 'identity' }, files: { project: fs.createReadStream('tests/fixtures/test.json') } }) response.on('end', function() { response._getData().should.equal('finished'); done(); }) app.handle(request, response) 任何想法为什么这不会导致适当的file upload? 至less,express-fileupload模块没有看到它。 非常感谢。

存根内部function?

我想在unit testing时在我的代码中存储一个内部函数,例如: //foobar.js const uuid = require('uuid'); function foo() { console.log('uuid: ' + uuid.v4()); // Lots of timers } exports._foo = foo; function bar() { //Logic… foo(); //Logic… } exports.bar = bar; 而unit testing: // test/foobar.js const chai = require('chai'), expect = chai.expect, proxyquire = require('proxyquire'), sinon = require('sinon'); describe('bar', () => { it('call foo', […]

Sinon称为With和calledWithMatch失败的对象编辑:Sinon间谍对象的构造函数

我是unit testing新手,正在使用Mocha,Sinon和Chai来testingNodeJs代码。 问题是我对stub.calledWith()的期望总是失败,即使testing错误显示了两个在语法上相同的对象。 我使用Sinon来存储一个mongoose模型的保存方法,并检查这个存根被调用了正确的细节。 我正在testing的function: async function createGroup(details) { 'use strict'; Logger.info(`create group called`); const group = new UserGroup(this.formatGroup(details)); const result = await group.save(); Logger.verbose(`results of save: ${JSON.stringify(result)}`); return result; } unit testing describe('object creation', function () { 'use strict'; const saveStub = sinon.stub(UserGroup.prototype, 'save'); mongoose.Promise = Promise; beforeEach(function (done) { saveStub.reset(); return done(); }); […]

在一个自定义的NodeJS包中,如何对依赖于同一个包中的函数的函数进行unit testing?

我是unit testing的新手。 我正在对自定义NodeJS包中的每个函数执行unit testing。 假设我的包出口function如下, //my-package.js module.exports = { listFiles, copyFiles } /** * @return <list> a list of files under src_dir */ function listFiles(src_dir){ //stat all files under src_dir and make a list containing all file names … } /** * @param [allowed_ext] a list of file extensions */ function copyFiles(src_dir, dst_dir, allowed_ext = […]

如何debugging与Mocha运行的Eclipse中的Javascriptunit testing?

我可以用这个命令运行我的Mocha / Unit.jsunit testing: mocha fooTest.js 我想在Eclipse中设置断点并debugging这个unit testing,但是我只在Eclipse中看到以下debuggingconfiguration选项。 有没有办法在Eclipse的debugging模式下执行摩卡unit testing? 我正在使用Eclipse的JavaScript和Web开发人员氧气释放(4.7.0) 。 也许有一个插件,我可以安装或者可能创build一个node.jsdebuggingconfiguration,并使其与摩卡运行? 或者有没有办法在IntelliJ IDE中做到这一点?

testingexpression中间件

我有以下代码来testing: const Status = require('http-status-codes'); const passport = require('passport'); const Users = require('../models/users.js'); const authentication = { // Authenticate User Middleware authenticateUser: function authenticateUser(req, res, next) { return passport.authenticate('bearer', { session: false, failWithError: false }, (err, user, info) => { if (err) { return res.status(500).json({ message: err }); } if (user) { return Users.findOne({ auth_ref: […]

Node.js中定义的AssertionError在哪里?

我希望我的unit testing断言特定的函数调用会在需要时抛出一个AssertionError,而不是抛出一个exception。 assertion库(expect)通过传入一个exception构造函数来支持这样的事情,但是我似乎无法findAssertionError构造函数被导出的地方。 它只是为了成为一个内部阶级,而不是暴露给我们? 文档包含大量的参考,但没有链接。 我有一个超级哈克的方式: let AssertionError; try { const assert = require("assert"); assert.fail(); } catch (ex) { AssertionError = ex.constructor; } 但我希望有更好的方法。

在node.js上unit testingmysql回滚

我在一个节点项目中使用mysql。 我想unit testing一个JavaScript函数,使一个SQL交易。 如果事务成为locking监视器的受害者,则该函数具有处理失败的代码。 还是呢? 因为我是unit testing,所以我只在一个本地数据库上进行一次事务处理,所以不会出现死锁,对吗? 我怎样才能testing死锁处理,如果它永远不会发生? 有没有办法强制它发生? 例: thisMustBeDoneBeforeTheQuery(); connection.queryAsync(/*This is an update*/).catch(function(err) { undoThatStuffIDidBeforeTheQuery(); // I hope that function worked, because my unit tests can't // make a deadlock happen, so I can't know for sure. }