Tag: sinon

当使用Sinon的假定时器时,蓝鸟承诺冻结

与Sinon的假定时器和Bluebird一起使用时,以下testing会冻结。 var sinon = require('sinon'); var Promise = require('bluebird'); describe('failing test', function() { beforeEach(function() { this.clock = sinon.useFakeTimers(); }); afterEach(function() { this.clock.restore(); }); it('test', function(done) { Promise.delay(1000).then(function(){ done(); //This never gets called }); }); }); 我正在用Bluebird(v2.9.33)和Sinon(v1.15.3)使用Mocha(v2.2.5)。 我尝试了Bluebird和Sinon在一些讨论中提出的build议,但是我无法做到这一点。 这似乎是一个问题与方式Sinon存根setImmediate但除此之外,我不知道如何解决这个问题。

存根Date.now()和Math.random()

我正在用Sinon使用Mocha来testing我的node.js模块。 我已经成功地嘲笑了其他的依赖(我写的其他模块),但是我遇到了困扰非纯函数(如Math.random()和Date.now() )的问题。 我已经尝试了以下(简化,以便这个问题不是如此本地化),但Math.random()不存在,因为一个明显的范围问题。 Math的实例在testing文件和mymodule.js之间是独立的。 test.js var sinon = require('sinon'), mymodule = require('./mymodule.js'), other = require('./other.js'); describe('MyModule', function() { describe('funcThatDependsOnRandom', function() { it('should call other.otherFunc with a random num when no num provided', function() { sinon.mock(other).expects('otherFunc').withArgs(0.5).once(); sinon.stub(Math, 'random').returns(0.5); funcThatDependsOnRandom(); // called with no args, so should call // other.otherFunc with random num other.verify(); // ensure […]

删除模块function

编辑:有点更精确。 我想testing我们团队创build的Github API封装扩展的用例。 对于testing,我们不想直接使用API​​包装器扩展,所以我们想要把它的function截掉。 所有对API封装器的调用都应该为testing而被删除,而不仅仅是创build一个复制存根。 我在Node.js中有一个模块“github”: module.exports = function(args, done) { … } 我要求这样: var github = require('../services/github'); 现在,我想使用Sinon.js将github(…) var stub_github = sinon.stub(???, "github", function (args, callback) { console.log("the github(…) call was stubbed out!!"); }); 但是sinon.stub(…)期望从我传递一个对象和一个方法,并且不允许我sinon.stub(…)一个函数模块。 有任何想法吗?

用Sinon保存Mongoose模型的实例方法

我试图testing一个服务函数,我用它来保存一个使用Mongoose模型的小部件。 我想在我的模型上存储保存实例方法,但是我找不出一个好的解决scheme。 我看到了其他的build议,但没有一个看起来完整。 看… 这个 , 这个 。 这是我的模特 // widget.js var mongoose = require('mongoose'); var widgetSchema = mongoose.Schema({ title: {type: String, default: ''} }); var Widget = mongoose.model('Widget', widgetSchema); module.exports = Widget; 这是我的服务… // widgetservice.js var Widget = require('./widget.js'); var createWidget = function(data, callback) { var widget = new Widget(data); widget.save(function(err, doc) { callback(err, […]

在NodeJS中使用Mocha和Sinon来ES6类的方法

有没有办法使用摩卡/ Sinon存根ES6类的方法? 我正在尝试这样做… sinon.stub(Factory, 'announce'); 但我只是得到以下错误… TypeError: Attempted to wrap undefined property announce as function

使用AWS-SDK的Sinon.Stub节点

我正在尝试为使用aws-sdk NPM模块的应用程序编写一些testing报告,将aws-sdk模块推到SQS队列,但我不确定如何正确地模拟事情。 到目前为止,我的testing是: var request = require('superagent'), expect = require('chai').expect, assert = require('chai').assert, sinon = require('sinon'), AWS = require('aws-sdk'), app = require("../../../../app"); describe("Activities", function () { describe("POST /activities", function () { beforeEach(function(done) { sinon.stub(AWS.SQS.prototype, 'sendMessage'); done(); }); afterEach(function(done) { AWS.SQS.prototype.sendMessage.restore(); done(); }); it("should call SQS successfully", function (done) { var body = { "custom_activity_node_id" : […]

如何使用Rollup.js捆绑使用Sinon.js的testing?

我使用Rollup.js和插件rollup-plugin-node-resolve和rollup-plugin-commonjs来捆绑我使用Sinon.js的testing。 当我尝试运行捆绑文件时,出现以下错误: 错误:rollup-plugin-commonjs当前不支持dynamic需求 有没有任何解决这个错误,或者我必须使用一些其他工具,如Webpack?

用sinon和bluebird来保存一个promisifiedfunction

在我想testing的文件中,我有以下代码: var httpGet = Promise.promisify(require("request").get); httpGet(endpoint, { auth: {bearer: req.body.access_token}, json: true }) .then(…) 现在,在我的testing中,我想确保httpGet被调用一次,并确保参数是有效的。 在promisified之前,我的testing看起来是这样的: beforeEach(function () { request.get = sinon.stub() .yields(null, null, {error: "test error", error_description: "fake google error."}); }); afterEach(function () { expect(request.get).to.have.been.calledOnce(); var requestArgs = request.get.args[0]; var uri = requestArgs[0]; expect(uri).to.equal(endpoint); //… }); 不幸的是,当request.get被promisified时,这不再起作用。 我尝试刷新request.getAsync而不是(因为蓝鸟附加“asynchronous”promisifiedfunction),但是这也不起作用。 有任何想法吗?

与Sinon一起testingaxios调用,还有redux和Karma

你好在testing的redux文档中,他们有这个例子来testingAPI调用: import configureMockStore from 'redux-mock-store' import thunk from 'redux-thunk' import * as actions from '../../actions/counter' import * as types from '../../constants/ActionTypes' import nock from 'nock' const middlewares = [ thunk ] const mockStore = configureMockStore(middlewares) describe('async actions', () => { afterEach(() => { nock.cleanAll() }) it('creates FETCH_TODOS_SUCCESS when fetching todos has been done', (done) => […]

在sinon.js中扼杀和/或嘲笑一堂课?

我为我的应用程序创build了一个数据库包装器,如下所示。 为了testing它,我显然希望replace实际的数据库库。 我可以创build一个新的类来嘲笑query方法,并捕获所有的input,但使用sinon.js似乎更合适,但我将如何使用它? sinon.js的mock或stubfunction是我应该使用的吗? wrapper = (function() { function wrapper() {} wrapper.db = require("database"); wrapper.prototype.insertUser = function(doc) { return this.db.query("INSERT INTO USERS…"); }; return wrapper; })();