sinon

Mock fs.readdir for testing

大憨熊 提交于 2019-12-05 06:37:51
I'm trying to mock the function fs.readdir for my tests. At first I've tried to use sinon because this is a very good framework for this, but is hasn't worked. stub(fs, 'readdir').yieldsTo('callback', { error: null, files: ['index.md', 'page1.md', 'page2.md'] }); My second attempt was to mock the function with a self-replaced function. But it also doesn't works. beforeEach(function () { original = fs.readdir; fs.readdir = function (path, callback) { callback(null, ['/content/index.md', '/content/page1.md', '/content/page2.md']); }; }); afterEach(function () { fs.readdir = original; }); Can

node js unit testing: mocking require dependency

不想你离开。 提交于 2019-12-05 04:38:19
I am having issues writing unit test for the following setup as a jira.js file (in a node.js module): var rest = require('restler'); // https://www.npmjs.com/package/restler module.exports = function (conf) { var exported = {}; exported.getIssue = function (issueId, done) { ... rest.get(uri).on('complete', function(data, response) { ... }; return exported; }; Now, i want to write unit test for my getIssue function. 'restler' is a REST client through which i make REST calls to the JIRA API to get a JIRA issue via my code. So to be able to test createIssue(..), I want to be able to mock the

How to assert stubbed fetch more than once

戏子无情 提交于 2019-12-05 04:38:11
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)=> { console.log('error') }); } function b() { fetch('https://www.google.com') .then((e) => { console.log(

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

好久不见. 提交于 2019-12-05 00:00:40
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 immediately) the animation visibly occurs. In IE, Opera and Safari, the page loads, the animation function is

TypeError: 'undefined' is not a function (evaluating 'sinon.spy()')

▼魔方 西西 提交于 2019-12-04 22:44:34
I'm trying to use sinon.js in testing of a backbone application. But unfortunately I cannot use spy method due to error: TypeError: 'undefined' is not a function (evaluating 'sinon.spy()') Here is the steps to reproduce the error: Create an empty project with backbone yeoman generator Install sinon: cd test && bower install sinon Include in test/index.html <script src="bower_components/sinon/lib/sinon.js"></script> Create spy in test/spec/test.js: describe('Give it some context', function () { describe('maybe a bit more context here', function () { it('should run here few assertions', function

Javascript: Mocking Constructor using Sinon

本小妞迷上赌 提交于 2019-12-04 22:13:32
I am pulling my hair out trying to figure out how to mock a constructor using sinon. I have a function that will create multiple widgets by calling a constructor that accepts a few arguments. I want to verify that the constructor is called the correct number of times with the correct parameters, but I don't want to actually construct the widgets. The following links seemingly explain a straightforward way of mocking the constructor, however it does not work for me: Spying on a constructor using Jasmine http://tinnedfruit.com/2011/03/25/testing-backbone-apps-with-jasmine-sinon-2.html When I

Does jasmine need sinon.js?

余生颓废 提交于 2019-12-04 15:33:38
问题 I've seen examples on the web in which people use jasmine together with sinon. However, jasmine has support for spies (which as I understand is what Sinon does). So, the question is, is Sinon still useful when using Jasmine ? If Sinon is useful what exactly makes it a good addition to jasmine ? Cheers 回答1: No you dont need Sinon to work with Jasmine. But Sinon spy/mock/stubs are more convenient in some cases. There is also a minor bug in mocking setTimeout in Jasmine, which work as expected

How do I test Ember.run.later with Sinon?

久未见 提交于 2019-12-04 08:41:32
I have some Ember code that sets a timeout: var MyObject = Ember.Object.extend({ setFooToBarLater: function() { Ember.run.later(this, 'set', 'foo', 'bar', 500); } }); I'd like to test that using Sinon's fake clock. Here's what I tried: var clock = sinon.useFakeTimers(); var myObject = MyObject.create(); myObject.setFooToBarLater(); clock.tick(600); expect(myObject.get('foo')).to.be('bar'); But the expect always runs before the set . I also tried wrapping the clock.tick in a run-loop: Ember.run(clock, 'tick', 600); Beware that sinon.useFakeTimers (by default on) overrides the window.Date. That

stubbing ES6 super methods using sinon

风格不统一 提交于 2019-12-04 05:52:05
I am having a problem stubbing the base class methods using Sinon. In the example below I am stubbing the call to base class method GetMyDetails as follows. I am sure there is a better way. actor = sinon.stub(student.__proto__.__proto__,"GetMyDetails"); And also the value of this.Role ends up being undefined. I have created a simple class in javascript "use strict"; class Actor { constructor(userName, role) { this.UserName = userName; this.Role = role; } GetMyDetails(query,projection,populate,callback) { let dal = dalFactory.createDAL(this.Role); dal.PromiseFindOneWithProjectionAndPopulate

Problems with ava asynchronous tests when stubbing with sinon

ε祈祈猫儿з 提交于 2019-12-04 05:51:56
I have a couple of tests I'd like to run on the .then and .catch blocks of one of my dependencies. import test from 'ava'; import sinon from 'sinon'; // Fake dependency code - this would be imported const myDependency = { someMethod: () => {} }; // Fake production code - this would be imported function someCode() { return myDependency.someMethod() .then((response) => { return response; }) .catch((error) => { throw error; }); } // Test code let sandbox; test.beforeEach(() => { sandbox = sinon.sandbox.create(); }); test.afterEach.always(() => { sandbox.restore(); }); test('First async test',