sinon

Use sinon.js to create a “spy object” with spy methods based on a real constructor/prototype

走远了吗. 提交于 2019-12-01 15:34:10
I'm using sinon.js as a way to stub out dependencies in my Mocha tests. I prefer the 'spy' approach over a classic mock approach, as the introspection of the spy seems clearer and affords more flexibility than the somewhat backward-thinking with classic mock objects. That said, I wonder if I'm using it incorrectly when it comes to creating test spies for whole objects. Let's say I have a test dependency that has 4 methods on it and I want to stub each of those methods and make assertions on one or two of them. Currently I'm doing this: var spyObj = { aMethod: sinon.spy(), otherMethod: sinon

Sinon Spy is not called if the spied method is called indirectly

江枫思渺然 提交于 2019-12-01 14:54:07
问题 Problem In our codebase we have a problem with sinon which can be reproduced with the code snipped below. The thing is that it seems to be that indirect called spies return force false , the console.log clearly states that the method is called but the spy.called remains false . Code The following CDN's can be used for the html: //cdnjs.cloudflare.com/ajax/libs/sinon.js/1.7.3/sinon-min.js //cdnjs.cloudflare.com/ajax/libs/require.js/2.1.14/require.min.js main.js require(['myModule'], function

Use sinon.js to create a “spy object” with spy methods based on a real constructor/prototype

大城市里の小女人 提交于 2019-12-01 14:30:25
问题 I'm using sinon.js as a way to stub out dependencies in my Mocha tests. I prefer the 'spy' approach over a classic mock approach, as the introspection of the spy seems clearer and affords more flexibility than the somewhat backward-thinking with classic mock objects. That said, I wonder if I'm using it incorrectly when it comes to creating test spies for whole objects. Let's say I have a test dependency that has 4 methods on it and I want to stub each of those methods and make assertions on

Sinon FakeServer no requests?

假装没事ソ 提交于 2019-12-01 14:01:59
I'm following the SinonJS Fake Server tutorial and I'm running this simple code: var server; before(function () { server = sinon.fakeServer.create(); }); after(function () { server.restore(); }); it("calls callback with deserialized data", function () { var callback = sinon.spy(); getTodos(42, callback); // This is part of the FakeXMLHttpRequest API server.requests[0].respond( 200, { "Content-Type": "application/json" }, JSON.stringify([{ id: 1, text: "Provide examples", done: true }]) ); assert(callback.calledOnce); }); I'm including the getTodos function in the same file. Here it is as well:

Sinon FakeServer no requests?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 12:28:20
问题 I'm following the SinonJS Fake Server tutorial and I'm running this simple code: var server; before(function () { server = sinon.fakeServer.create(); }); after(function () { server.restore(); }); it("calls callback with deserialized data", function () { var callback = sinon.spy(); getTodos(42, callback); // This is part of the FakeXMLHttpRequest API server.requests[0].respond( 200, { "Content-Type": "application/json" }, JSON.stringify([{ id: 1, text: "Provide examples", done: true }]) );

Stubbing window.location.href with Sinon

大憨熊 提交于 2019-12-01 02:12:00
I am trying to test some client-side code and for that I need to stub the value of window.location.href property using Mocha/Sinon. What I have tried so far ( using this example ): describe('Logger', () => { it('should compose a Log', () => { var stub = sinon.stub(window.location, 'href', 'http://www.foo.com'); }); }); The runner displays the following error: TypeError: Custom stub should be a function or a property descriptor Changing the test code to: describe('Logger', () => { it('should compose a Log', () => { var stub = sinon.stub(window.location, 'href', { value: 'foo' }); }); }); Which

Sinon not stubbing on module.exports

。_饼干妹妹 提交于 2019-12-01 00:26:17
If create an file with the following contents const validateEmail = email => { sendEmail(email); }; const sendEmail = email => { return true; }; module.exports = { validateEmail, sendEmail, }; And a test that tries to stub out the second function... it('Should call sendEmail if a valid email is passed', () => { let sendEmailSpy = sinon.stub(checkEmail, 'sendEmail'); checkEmail.validateEmail('acorrectemail@therightformat.com'); assert.isTrue(sendEmailSpy.called); }); It still calls the sendEmail function and the test fails However, if I write the module.exports like this: module.exports = {

node express es6 sinon stubbing middleware not working

夙愿已清 提交于 2019-11-30 22:58:03
I am writing the mocha unit test for the my express router. I found that however I try to stub the middleware, it still execute the middleware code. Here is my router & test, could anyone figure out? Router: import { aMiddleware, bMiddleware, cMiddleware } from '../middleware.js'; router.post('/url', aMiddleware, bMiddleware, cMiddleware, function(req, res) { ... } Middleware: AuthMiddleware.aMiddleware = async (req, res, next) => { console.log('in real middleware'); next(); } Test: var authMiddleware = require('../../middleware/auth.js'); describe('Test', async () => { before(function (done)

Stubbing window.location.href with Sinon

折月煮酒 提交于 2019-11-30 22:29:37
问题 I am trying to test some client-side code and for that I need to stub the value of window.location.href property using Mocha/Sinon. What I have tried so far (using this example): describe('Logger', () => { it('should compose a Log', () => { var stub = sinon.stub(window.location, 'href', 'http://www.foo.com'); }); }); The runner displays the following error: TypeError: Custom stub should be a function or a property descriptor Changing the test code to: describe('Logger', () => { it('should

Sinon does not seem to spy for an event handler callback

北城余情 提交于 2019-11-30 22:10:04
I'm testing a backbone view with Jasmin, Simon and jasmin-simon. Here is the code: var MessageContainerView = Backbone.View.extend({ id: 'messages', initialize: function() { this.collection.bind('add', this.addMessage, this); }, render: function( event ) { this.collection.each(this.addMessage); return this; }, addMessage: function( message ) { console.log('addMessage called', message); var view = new MessageView({model: message}); $('#' + this.id).append(view.render().el); } }); Actually, all my tests pass but one. I would like to check that addMessage is called whenever I add an item to this