sinon

Preventing AJAX call with Jasmine and Sinon using Backbone

北城余情 提交于 2019-12-10 11:17:08
问题 I'm just getting started testing my Backbone app with Sinon and Jasmine. I have a view that look something like (coffeescript): initialize: -> @collection.on 'reset', @render, this render: -> if @collection.fetched # do stuff else @$el.append "<h3>Loading...</h3>" @collection.fetch() this I want to test this with an unfetched collection, but I'm not sure how to fake an ajax call within my code (obviously can easily be done in the spec). I realize that I could just pass in a pre-fetched

Catching thrown errors with SinonJS

你离开我真会死。 提交于 2019-12-10 09:47:35
问题 I've got a method that may throw an Error, but I'm having trouble writing a SinonJS/Mocha/Should unit test case for this condition. Sample function under test: function testError(value) { if (!value) { throw new Error('No value'); return false; } }; Sample test: describe('#testError', function() { it('throws an error', function() { var spy = sinon.spy(testError); testError(false); spy.threw().should.be.true(); }); }); This outputs: #testError 1) throws an error 0 passing (11ms) 1 failing 1)

How to assert stubbed fetch more than once

余生长醉 提交于 2019-12-10 04:19:23
问题 Using proxyquire, sinon, and mocha. I am able to stub fetch on the first call of fetch. But on the second fetch call, which is recursive, I am not able to assert it. From the output, it looks like the assertion may run before the test finishes. You will see this with second fetch console out after assertion. index.js var fetch = require('node-fetch'); function a() { console.log('function a runs'); fetch('https://www.google.com') .then((e) => { console.log('first fetch'); b(); }) .catch((e)=>

sinon.js stub - can you call more than one callback on a single stubbed function?

☆樱花仙子☆ 提交于 2019-12-10 03:14:41
问题 If I have a stub for a function that takes 2 callbacks, how can I wire up sinon.js to call both callbacks when the stubbed function is invoked? For example - here's function that I want to stub which takes 2 functions as arguments: function stubThisThing(one, two) { ... one and two are functions ... ... contents stubbed by sinon.js ... } I can use sinon to call either one of the arguments : stubbedThing.callsArg(0); or stubbedThing.callsArg(1); but I can't seem to get both to be called . If I

How do I fake-time a jQuery animation using Sinon in a Jasmine unit test?

风流意气都作罢 提交于 2019-12-10 01:09:05
问题 I have a 1 second jQuery .animate action that launches 5 seconds after page load. I set up a Sinon timer in my Jasmine unit testing code and test after a tick of 7 seconds to see if the post-animation properties are as they should be. It doesn't work right, so I've placed an instance of the animation itself on my Jasmine HTML test page to better see what's going on. In Firefox and Chrome, the page loads, the animation function is called, the unit test immediately fails, and then (also

How do you stub private functions when unit testing a revealing module

非 Y 不嫁゛ 提交于 2019-12-09 12:53:55
问题 I have been building a node module that wraps a number of calls to the GitHub API and in my infinite wisdom have built this using the revealing module pattern, keeping my wrapping functions private and only exposing simple methods. See the example below: github.shortcuts = (function(){ var appPath; var createRepo = function(name){ var deferred = Q.defer(); github.repos.create({ name: name, auto_init: true }, function(error, result){ if (error) { deferred.reject(new Error(error)); } else {

Calling original function from Sinon.js Stub

爷,独闯天下 提交于 2019-12-09 07:51:38
问题 I'm trying to intercept a call with Sinon.js so I can do some logging and then execute the original call. I don't see a way to do this with sinon.spy(), but I think I can do it with sinon.stub(). I provided a custom function: sinon.stub(servicecore.ServiceWrapper.prototype, '_invoke', function(method, name, body, headers, callback) { console.log('---- ServiceWrapper._invoke called! ----'); // How do I call the original function? }); The problem I have is executing the original function, so my

How to stub process.env in node.js?

好久不见. 提交于 2019-12-09 04:27:37
问题 I want to stub process.env.FOO with bar . var sinon = require('sinon'); var stub = sinon.stub(process.env, 'FOO', 'bar'); I'm confused. I read document, but still I don't understand yet.sinonjs docs sinonjs is one example, not sinonjs is okay. 回答1: From my understanding of process.env , you can simply treat it like any other variable when setting its properties. Keep in mind, though, that every value in process.env must be a string. So, if you need a particular value in your test: it('does

Stubbing method in same file using Sinon

泪湿孤枕 提交于 2019-12-09 03:31:14
问题 I'm trying to unit test a function in a file while stubbing another function in the SAME file, but the mock is not being applied and the real method is being called. Here's an example: // file: 'foo.js' export function a() { // ..... } export function b() { let stuff = a(); // call a // ...do stuff } And my test: import * as actions from 'foo'; const aStub = sinon.stub(actions, 'a').returns('mocked return'); actions.b(); // b() is executed, which calls a() instead of the expected aStub() 回答1:

Testing Restify Route Handler that contains Promise Code Block, using SinonJs and Mocha

安稳与你 提交于 2019-12-08 21:23:26
I have a restify action code block below: function retriveAll(req, res, next) { db.user .find({where: {id: 1}) .then(function(user){ res.send(user); }) .catch(function(details){ res.send(details.message); }) .finally(function(){ next(); }); } I want to test this action specifically validating that res.send() was called within this code block . And later on validating the res.send() returned data. I'm using SinonJs and Mocha for testing framework. Here's a sample test code block for the method above. describe('retrieveAll()', function() { reqStub = {}; resStub = {send: sinon.stub()}; nextStub =