sinon

Catching thrown errors with SinonJS

拜拜、爱过 提交于 2019-12-05 21:41:12
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) #testError throws an error: Error: No value at testError (tests/unit/js/test-error.js:6:14) at Context.

How to use Fake timers with nightwatch.js and sinon.js?

旧巷老猫 提交于 2019-12-05 18:40:35
I'm doing JavaScript e2e test with nightwatch.js, and I want to mock the clock with sinon.js's fake timer http://sinonjs.org/docs/#clock But test stops before finish it, I got the log like below, and doesn't progress anymore. [some test] Test Suite =============================== ✔ Element <body> was visible after 5000 milliseconds. My test code is like below. How can I solve the problem? Thank you. module.exports = { before: function(browser) { clock = sinon.useFakeTimers(new Date(2015, 7, 20).getTime()); }, after: function(browser) { clock.restore(); browser.end(); }, 'some test': function

How do I stub a chain of methods in Sinon?

爱⌒轻易说出口 提交于 2019-12-05 18:07:00
问题 I know how to use stub to replace one function. sandbox.stub(Cars, "findOne", () => {return car1 }); But now I have a line in my function I want to test that I need to stub that looks like this Cars.find().fetch() So there is a chain of function here and I'm unsure what I need to do. How do I stub "find" to return something that I can use to stub "fetch"? 回答1: Try this: sandbox.stub(Cars, "find", () => { return { fetch: sinon.stub().returns(anything); }; }); 回答2: IMHO, we can just use returns

Sequelize Model Unit Test

半世苍凉 提交于 2019-12-05 14:46:57
I have a User sequelize model that has a beforeCreate hook that encrypts the password using bcrypyt . Bcrypyt is loaded as a dependency by the model using a require statement. Now, I'm writing my tests for my models and I want to write a test that ensures bcrypt is hashing the password on create. At the moment, I have added a setter onto the User model that sets the bcrypt object. In my tests, I can then create a spy using sinon and inject the spy using the setter and ensure it is called on create. Is this the correct way to do it? I feel as if I'm creating a setter purely for my tests and

sails.js + mocha + supertest + sinon: how to stub sails.js controller function

僤鯓⒐⒋嵵緔 提交于 2019-12-05 14:17:05
I am trying to stub a sails controller function, but I don't know which object to stub. using sinon.stub(object,'funcname', function()... This is probably related to the way sails bind controller functions... Here is some code to give example Controller file api/controllers/PersonController.js var fs = require('fs'); // // I want to stub retrieveData function when testing // function retreiveData(cb) { fs.readFile('./filedata', function (err, data) { if (err) throw err; cb(data.toString()); }); }; function showdata(req, res) { var stack = new Error().stack console.log( stack ) retreiveData

How to load a JS file that is not a module in dojo?

前提是你 提交于 2019-12-05 11:14:33
I'll start by saying that I am a javascript and dojo noob. However, I have been working on writing some unit tests for my js code, using the D.O.H framework. One thing I noticed is that the framework does not seem to have a way to mock XHR requests. So I decided to use sinon for the mocking. Heres my question, I cannot successfully load the sinon code into my dojo module. Here is what I tried: define(["doh/runner", "tests/sinon-1.4.2"], function(doh, sinnon) { ... }); I have the tests package mapped to the correct directory, and can load other files from there. So how do I go about loading

correct usage of sinon's fake XMLHttpRequest

半城伤御伤魂 提交于 2019-12-05 10:23:25
I am creating XMLHttpRequest javascript module to get JSON data from server. Here is the code: (function() { var makeRequest = function(url,callback,opt) { var xhr; if (XMLHttpRequest) { // Mozilla, Safari, ... xhr = new XMLHttpRequest(); } else if (ActiveXObject) { // IE try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!xhr) { callback.call(this, 'Giving up :( Cannot create an XMLHTTP instance', null); return false; } xhr.onreadystatechange = function() { if (xhr.readyState === 4) { if (xhr.status === 200

Mocking/Stubbing `super` calls

隐身守侯 提交于 2019-12-05 09:58:19
I would like to mock out super calls, especially constructors in some ES6 classes. For example import Bar from 'bar'; class Foo extends Bar { constructor(opts) { ... super(opts); } someFunc() { super.someFunc('asdf'); } } And then in my test, I would like to do something like import Foo from '../lib/foo'; import Bar from 'bar'; describe('constructor', function() { it('should call super', function() { let opts = Symbol('opts'); let constructorStub = sinon.stub(Bar, 'constructor'); new Foo(opts); sinon.assert.calledWith(constructorStub, opts); }); }) describe('someFunc', function() { it('should

Javascript: Mocking Constructor using Sinon

左心房为你撑大大i 提交于 2019-12-05 08:26:20
问题 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

Mock a class method using mockery and sinon

☆樱花仙子☆ 提交于 2019-12-05 08:12:16
I'm learning to unit test using the node module mockery with sinon. Using only mockery and a plain class I'm able to inject a mock successfully. However I would like to inject a sinon stub instead of a plain class but I'm having a lot of troubles with this. The class I am trying to mock: function LdapAuth(options) {} // The function that I want to mock. LdapAuth.prototype.authenticate = function (username, password, callback) {} And here is the code I'm currently using in my beforeEach() function: beforeEach(function() { ldapAuthMock = sinon.stub(LdapAuth.prototype, "authenticate", function