sinon

How to mock a function inside another function (which I am testing) using sinon?

前提是你 提交于 2019-12-06 18:31:33
问题 let's say i have a function Func a() { //Do Something let c = b(); return c; } I want to test the function a and mock b() and in the mock want to assign c. Sinon.Stub(Test,"b").returns("DummyValue"); c should be assigned DummyValue. How can I do that? describe("a", () => { let a = a(); //mock b(); action = execute(a); expect(action).should.return.("DummyValue"); }) 回答1: When we have 2 functions in the same file and want to stub one of them and test the other. For example,: Test: tests.js let

Testing Ember Simple Auth in Ember App Kit with sinon

十年热恋 提交于 2019-12-06 14:00:07
I would like to mock server login responses when testing Ember Simple Auth in my Ember App Kit application. However, with the following code I get an opaque error 'Unexpected end of input' when the click action is called on the visit function : var App; module('Acceptances - SignIn', { setup: function(){ App = startApp(); this.xhr = sinon.useFakeXMLHttpRequest(); this.server = sinon.fakeServer.create(); this.server.autoRespond = true; sinon.spy(Ember.$, 'ajax'); this.server.respondWith('POST', '/oauth/token', [ 200, { 'Content-Type': 'application/json' }, '{"access_token":"secret token 2!",

Sinon/Mocha test with async ajax calls didn't return promises

时光怂恿深爱的人放手 提交于 2019-12-06 13:13:38
I'm writing some test for my client side api use karma with Mocha and Sino . But I'm stuck on getting the async process. import api from '../../../src/api'; import stubData from '../data'; import axios from 'axios'; /* eslint-disable prefer-arrow-callback,func-names */ describe('API test', function () { before(function () { this.server = sinon.fakeServer.create(); }); after(function () { this.server.restore(); }); it('Should return cart with provided token', function (done) { this.server.respondWith("GET", "/cart", [200, { "Content-Type": "application/json" }, '[{ "id": 12, "comment": "Hey

When using rewire and sinon fakeTimer, order matters?

纵然是瞬间 提交于 2019-12-06 06:25:25
While testing a setup with a timed interval, I came across this problem. First of all, I am using sinon's fakeTimers to create the right timed environment. rewire is used as dependency injection library. The problem is, that sometimes applying the fake timer seems to fail when rewire is involved while in some other instances it's perfectly working. Please check out this setup: test.js 'use strict'; require('should'); var sinon = require('sinon'); var rewire = require('rewire'); // this sample will not fall under the fake timer var SampleGlobal = rewire('./testmodule'); describe('Sinon fake

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

醉酒当歌 提交于 2019-12-06 03:48:16
问题 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

Asserting function calls inside a promise

孤街浪徒 提交于 2019-12-06 03:44:51
I'm writing some tests for an async node.js function which returns a promise using the Mocha, Chai and Sinon libraries. Let's say this is my function: function foo(params) { return ( mkdir(params) .then(dir => writeFile(dir)) ) } mkdir & writeFile are both async functions which return promises. I need to test that mkdir is being called once with the params given to foo . How can I do this? I've seen quite a few examples on how to assert the overall return value of foo ( sinon-as-promised is super helpful for that) but non about how to make sure individual functions are being called inside the

How to unit test localStorage using sinon

好久不见. 提交于 2019-12-06 01:57:39
I am trying to test localStorage using sinon. Basically I am very new to unit testing so this might be very basic. Update I managed to come up with this but now its giving me a new error Should wrap property of object Test describe('Initial State', () => { it('should set the initial state for the component', () => { const props = { currentUser: {} }; sinon.stub(window.localStorage, 'setItem'); window.localStorage.setItem('none', 'nothing'); }); }); Umair Sarfraz I managed to resolve it. Thanks to @anoop because his answer was of help but I had to manage it with a major workaround. I am using

Problems with ava asynchronous tests when stubbing with sinon

家住魔仙堡 提交于 2019-12-06 01:31:27
问题 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

Mocking http requests in node using mocha and sinon

 ̄綄美尐妖づ 提交于 2019-12-05 23:34:27
问题 I have written a NodeJS app using express that proxies some calls to external APIs. So I am trying to write a unit test using Mocha and Sinon. My goal is to test the app without any internet connectivity so I am trying to mock the https requests and return mock replies. I'm having a problem that I can't find any examples or tutorials that fit my case. My node app listens on port 8081 for http requests and then proxies them to another site. I want to test my app without it having to actually

How to sinon spy module export utility functions

痞子三分冷 提交于 2019-12-05 22:53:19
In javascript (ES6), I have a utility module which just contains some functions, and then in the end of the file, I export them like so: module.exports = { someFunction1, someFunction2, someFunction3, } Then I want to write unit tests for those functions. Some of the functions depend on each other; they call each other in a way, that for example, someFunction1 might call someFunction2. There's no circular problems. Everything works well, until I need to spy that one of the functions is called. How can I do it? Currently I'm using Chai and Sinon. In the test file, I have imported the whole file