Tag: sinon

使用sinun mocks和nodeunit

我正在学习使用sinun与nodeunit ,特别是做嘲弄。 推荐的方法是使用sinon-nodeunit 。 根据文档 ,mock应该可以通过对每个testing函数的test参数的mock方法来获得。 但是,这似乎并不奏效。 如果我在包含以下内容的文件上运行nodeunit – require('sinon-nodeunit'); exports['test sinon-nodeunit'] = function (test) { mock = test.mock({}); test.done(); }; – 我得到这个: $ nodeunit test/test-sinon-nodeunit.js test-sinon-nodeunit.js ✖ test sinon-nodeunit TypeError: Object #<Object> has no method 'mock' at /home/malkovich/test/test-sinon-nodeunit.js:4:17 at Object.runTest (/usr/local/lib/node/.npm/nodeunit/0.5.1/package/lib/core.js:54:9) at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/lib/core.js:90:21 at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:508:13 at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:118:13 at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:134:9 at /usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:507:9 at Object.concatSeries (/usr/local/lib/node/.npm/nodeunit/0.5.1/package/deps/async.js:147:23) […]

每次调用时,在sinon中扼杀函数返回不同的值

我有一个function如下所示: function test(parms) { var self = this; return this.test2(parms) .then(function (data) { if (data) { return ; } else { return Bluebird.delay(1000) .then(self.test.bind(self, parms)); } }.bind(self)); }; 我正在为这个函数编写unit testing。 我正在使用sinon.stub来模拟函数test2的function。 我写了一个testing用例,其中test2返回true ,因此test函数成功完成执行。 不过,我想要一个testing用例,在第一个实例test2返回false ,它等待延迟,下次test2返回true 。 为此我写了我的testing用例如下: var clock; var result; var test2stub; var count = 0; before(function () { clock = sinon.useFakeTimers(); //object is […]

第二个sinon存根未被asynchronous并行调用

我试图testing下面的代码: var archive = function(callback){ call to this.archive() here… } var map = function(callback){ call to this.map() here… } async.parallel([map, archive], function(error){ handle errors here… }) 我正在testing句柄错误function是通过存档和映射function,以便其中一个返回错误: var mapStub = sinon.stub(MyClass.prototype, 'map').yields("mock error",null ); var archiveStub = sinon.stub(MyClass.prototype, 'archive').yields(null,null ); 我遇到的问题是,archivestub似乎并没有被使用,因为我从该函数调用的函数(如果该函数被调用,因为我没有初始化variables的testing)。 我有另一个testing,其中存档函数返回一个错误,而不是地图函数,这个testing通过,而不是调用任何残桩方法,而不是存根。 var mapStub = sinon.stub(MyClass.prototype, 'map').yields(null,null ); var archiveStub = sinon.stub(MyClass.prototype, 'archive').yields("mock error",null );

Sinon间谍function不起作用

根据sinon.js的文档,我可以这样做: var spy = sinon.spy(myFunc); ,但它不起作用。 这是我的努力: var sinon = require("sinon"); describe('check bar calling', function(){ it('should call bar once', function() { var barSpy = sinon.spy(bar); foo("aaa"); barSpy.restore(); sinon.assert.calledOnce(barSpy); }); }); function foo(arg) { console.log("Hello from foo " + arg); bar(arg); } function bar(arg) { console.log("Hellof from bar " + arg); }

Sinon:强制回拨呼叫

我正在用这个代码testing一个函数: return new Promise((ok, fail) => { this.repository.findById(id, (error, result) => { if (error) return fail(error); ok(result); }); }); 我想testing失败的path,即,当findById方法调用带有错误的callback。 我正在使用sinon为我的仓库和它的findById方法生成一个存根,但我不知道如何强制存根调用所需的参数的callback 有人做过这样的事吗? 谢谢

unit testing用Mocha返回承诺的多个asynchronous调用

我想了解如何最好的unit testing我的asynchronousCommonJS模块。 在处理多重链式承诺时,我正在努力理解最佳实践。 让我们假设我有以下模块定义: module.exports = function(api, logger) { return api.get('/foo') .then(res => { return api.post('/bar/' + res.id) }) .then(res => { logger.log(res) }) .catch(err => { logger.error(err) }) } 我有以下spec文件试图testing正确的调用。 var module = require('./module') describe('module()', function() { var api; var logger; var getStub; var postStub; beforeEach(function() { getStub = sinon.stub(); postStub = sinon.stub(); api […]

用Node.js中的Sinon.js嘲笑Postgres进行unit testing

我有困难得到我的头如何我可以使用sinon嘲笑postgres这是我正在testing的模块所需的,或者如果它甚至是可能的。 我不是试图testingpostgres模块本身,只是我的对象,以确保它按预期工作,而且它正在调用它应该在这种情况下调用。 我想这个问题是需要设置节点,因为我的模块需要postgres模块来打数据库,但在这里我不想运行一个集成testing,我只是想确保我的代码是孤立的工作,并不在乎数据库在做什么,我会把它留给我的集成testing。 我看到一些人设置他们的函数有一个可选的参数发送模拟/存根/假的function,testing它的存在,如果它在那里使用它在所需的模块,但这似乎像一个气味给我(我是新的节点,所以也许这不是)。 我宁愿嘲笑这一点,而不是尝试和劫持的要求,如果这是可能的。 一些代码 (请注意,这不是真正的代码,因为我使用TDD运行,并且该函数没有做任何事情,函数名称是真实的) testing设置 describe('#execute', function () { it('should return data rows when executing a select', function(){ //Not sure what to do here }); }); 示例function PostgresqlProvider.prototype.execute = function (query, cb) { var self = this; if (self.connection === "") cb(new Error('Connection can not be empty, set Connection using Init function')); […]

使用Sinon在javascript中扼杀Redis交互

我在node.js工作。 我的应用程序通过node_redis模块与Redis进行交互。 我正在使用摩卡和sinon来自动testing我的应用程序。 我的应用程序看起来像这样: …snip var redisClient = redis.createClient(redisPort, redisHost); var someValue = redisClient.get("someKey"); return someValue; …. 我想将呼叫存根到redisClient.get()。 要做到这一点,我也需要存根调用redis.createClient() – 我认为…这是我的testing代码: … var redis = require("redis"); var redisClient; … sinon.stub(redisClient, 'get').returns("someValue"); sinon.stub(redis, "createClient").returns(redisClient); … assert.equal(redis_client_underTest.call_to_redis(), "someValue"); … testing失败与AssertionError: false == "someValue" 我如何将redisClient存根,或者甚至可能呢?

这是在Node中做dependency injection的正确方法吗?

我最近启动了一个节点项目,作为一名testing驱动开发人员,我用我的全新模块很快遇到了dependency injection问题。 以下是我如何计算出我应该做dependency injection。 注意我使用誓言作为BDD框架并且用Sinon扩展它是重要的。 我的模块: exports.myMethod = function () { var crypto = exports.cryptoLib || require('ezcrypto').Crypto; crypto.HMAC( crypto.SHA256, 'I want to encrypt this', 'with this very tasty salt' ); }; 我的testing: var vows = require('vows'), sinon = require('sinon'); vows.describe('myObject').addBatch({ 'myMethod':{ 'topic':true, 'calls ezcrypto.HMAC':function () { var myObject = require('../playground.js'); var mock = sinon.mock(require('ezcrypto').Crypto); myObject.cryptoLib […]

为什么我得到“TypeError:无法读取财产'恢复未定义”当与sinon.js短语mongoose

我试图让我的脑袋周围,并与Sinon.js嘲笑mongoose和我得到的错误: TypeError: Cannot read property 'restore' of undefined 。 我曾尝试search谷歌和SO,但一直没有运气。 有人可以告诉我,我正在接近这个,如果是的话,我做错了,这个错误被抛出。 如果我这样做是错误的,我会赞赏一个正确的方向 这是我的模型/架构: var Mongoose = require("mongoose"); var Schema = Mongoose.Schema; exports = module.exports = function (host, database, port) { var mySchema = new Schema({ name: { type: String, required: true, unique: true }, addresses: { type: Array, required: false } }, { strict: true […]