Tag: sinon

如何重置/恢复到原来的产卵

我正在尝试使用模拟生成模块来模拟生成过程。 随后的testing失败了,因为我无法恢复模拟。 我试着用mySpawn.resotre()但没有这样的function。 我检查了代码仍然无法弄清楚。 任何人都可以请帮助我。

事件没有捕获我的testing与残桩function

我遇到了一个我不明白的问题。 我发出的事件在我的testing中没有被捕获。 这是下面的代码( event.js ): var util = require('util'), proc = require('child_process'), EventEmitter = require('events').EventEmitter; var Event = function() { var _self = this; proc.exec('ls -l', function(error, stdout, stderr) { _self.emit('test'); console.log('emitted'); }); }; util.inherits(Event, EventEmitter); module.exports = Event; 而相应的testing: var proc = require('child_process'), sinon = require('sinon'), chai = require('chai'), expect = chai.expect, Event = […]

嘲讽net.Socket为unit testing

我创build了一个包装对象与networking模块进行交互。 实现这个包装器的对象可以注册在net.Server发生任何事件时被调用的callback函数。 现在我试图testing这些callback,我不知道如何创build一个假的套接字,因为它来自一个新的连接,传递给callback。 有没有人有任何想法如何做到这一点? 非常感谢你。

在unit testing中,如何testing新缓冲区中创build的值?

假设我们有一些这样的代码: callFunction(new Buffer(foo)); 我们想用sinon.js间谍unit testing这个调用: var spy = sinon.spy(callFunction); expect(spy.to.have.been.calledWith(foo)); 这将失败,因为callFunction是用new Buffer(foo)而不是foo调用的。 如果我们expect(spy.to.have.been.calledWith(new Buffer(foo))testing通过expect(spy.to.have.been.calledWith(new Buffer(foo))但为每个testing创build新的缓冲区是否明智?有没有更好的方法来做到这一点?

如何用sinon.js将全局依赖项的新实例方法存留在nodejs中

对不起,标题混乱,我不知道如何更好地描述它。 让我们看看代码: var client = require('some-external-lib').createClient('config string'); //constructor function MyClass(){ } MyClass.prototype.doSomething = function(a,b){ client.doWork(a+b); } MyClass.prototype.doSomethingElse = function(c,d){ client.doWork(c*d); } module.exports = new MyClass(); testing: var sinon = require('sinon'); var MyClass = requre('./myclass'); var client = require('some-external-lib').createClient('config string'); describe('doSomething method', function() { it('should call client.doWork()',function(){ var stub = sinon.stub(client,'doWork'); MyClass.doSomething(); assert(stub.calledOnce); //not working! returns […]

使用nodejs,mocha,chai和sinon我想存根在路由中调用的函数

我有一个非常简单的应用程序,它是nodejs,express和mysql我是新的unit testing,我认为这个应用程序是一个很好的方法来做得更好。 我试图完成(我认为sinon是答案)是模拟或存根mysql.insertEventIntoDB和rabbit.addToRabbitMQ 在我的应用程序中,我有 app.use('/sendgrid', sendgrid(pool, config, logger) ); 在我的sendgrid.js中 var express = require('express'); var mysql = require('../app/utils/mysql'); var rabbit = require('../app/utils/rabbitMQ'); module.exports = function (dbpool, config, logger) { var router = express.Router(); router.post('/callback', function (req, res) { for (var x=0 ; x < req.body.length ; x++ ){ mysql.insertEventIntoDB(dbpool, req.body[x], logger); rabbit.addToRabbitMQ(config,req.body[x], logger) } res.json({ […]

如何使用sinon来模拟作为函数导出的节点模块的函数/属性?

我有一个作为function导出的服务模块。 我需要传递一些东西,像一个configuration对象,所以它需要保留这个结构。 我正在尝试从服务中删除一个函数,但无法弄清楚。 在我的应用程序,我有一个函数,使testing期间有问题的API调用,所以我想存根。 (我知道我必须写我的testing不同来处理asynchronous问题) // myService.js module.exports = function(config) { function foo() { returns 'bar'; } return { foo: foo }; }; // test.js var config = require('../../config'); var request = require('supertest'); var chai = require('chai'); var expect = chai.expect; var sinon = require('sinon'); var myService = require('./myService.js')(config); describe('Simple test', function(done) { it('should expect […]

在Express中为中间件testing提出请求\响应

我在Express应用程序中有一些中间件function,它们向一个单独的服务器发出请求,然后将返回的响应发送给客户端。 所以这件事情 var request = require('request'), helpers = require('./helpers.js'); //GET module.exports.fetch = function(req, res) { var options = helpers.buildAPIRequestOptions(req); request(options, function(err, response, body){ res.status(response.statusCode).send(body); }); }; 我正在编写unit testing,并希望模拟出来,并返回自己的回应,当传出的请求与这些function被解雇。 我遇到了超级烂摊子,并且写下了这样一个套餐: var app = require('./index.js'), request = require('supertest')(app), should = require('should'); //GET describe('GET requests', function(){ it('should do something', function(done){ request .get('/api/schemes') .expect(200, done) .end(function(err, res){ // console.log(res); […]

如何在内部调用的模块中存根/窥探外部方法?

//entity module function isUnique(name) { return true; } exports.isUnique = isUnique; function create(name) { if (isUnique(name)) { return console.log(name); } else { return new Error(); } } exports.create = create; 是否有可能残留“isUnique”方法来改变“创build”方法的行为? var entity = require("./entity"); var sinon = require("sinon"); var assert = require("assert"); var stub = sinon.stub(entity, "isUnique", function (name) { return false }); entity.create("test"); […]

模拟本地存储以testing两种方法的function/覆盖

以下代码使用本地存储工作正常,我想为它创buildunit testing的问题是,我在这个函数中使用本地存储 这是我想testing的function open: function(url) { var that = this; if (!localStorage.getItem(“alert”)) { that_run(url) .then(function(isBlocked) { if (isBlocked) { that._notify(); localStorage.setItem(“alert”, true); } else { localStorage.setItem(“alert”, true); that._exe(“page”,url) } }).done(); } else { that._exe(“page”,url) } }, 这是正在工作的testing,但我认为重写窗口是不好的做法和我的问题,如果我能写这个testing更好? it.only("test for open", function () { var url = "http://mytesturl”; winlocalStorage.getItem = function(){ return false; }; var oSimulateSpy […]