sinon-chai


sinon calledWith(new Error()) and with exact message

纵饮孤独 提交于 2020-01-24 04:28:13
问题 i need to test this function : //user.js function getUser(req,res,next){ helper.get_user(param1, param2, (err,file)=>{ if (err) return next(err);} my test function : it ("failed - helper.get_user throws error",sinon.test(function () { var req,res; var get_user = this.stub(helper,"get_user") get_user.yields(new Error("message")); var next = sinon.spy(next); user.get_user(req,res,next); expect(next).to.have.been.calledWith(new Error("other message")); })) for my assertion I'm using sinon-chai

How to get a sinon stub to call another function on nth call

天涯浪子 提交于 2020-01-07 05:46:27
问题 I want to use a sinon stub to asynchronously test an event emitter. I want the stub to call a callback after it is called. I thought stub.yields was what I want but not. Is there a neat way to do this? it('asynchronously emits finish after logging is complete', function(done){ const EE = require('events'); const testEmitter = new EE(); var cb = sinon.stub(); cb.calls(completed); // no such method but this is what I need testEmitter.on('finish', cb.bind(null)); testEmitter.emit('finish');

Sinon stubbing helper method defined in same file

僤鯓⒐⒋嵵緔 提交于 2019-12-23 09:52:37
问题 So I have a file, user-database , that looks something like this : export function foo(id: number): Promise<boolean> { return new Promise<boolean>((resolve, reject) => { findSomething(id) .then((data) => { //do something with data }) } } export function findSomething(id: number): Promise<Object> { return new Promise<Object> ((resolve, reject) => { let query = 'SELECT * FROM user'; db.executeQuery(query); .then(data) => { if(data.length < 1) { reject(new Error('whoops')); } resolve(data); },

How to unit test localStorage using sinon

时光毁灭记忆、已成空白 提交于 2019-12-22 09:48:45
问题 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'); }); }); 回答1: I managed to resolve it. Thanks to

sinonStub.called prints false, though I am calling the function

无人久伴 提交于 2019-12-13 20:58:59
问题 I am running test on node js app using sinon. I want the sinonStub.called to be true but this prints false. I am calling the function indirectly( function call within function). I have given code snippet below spec.js describe.only('creating stub for Accounts method',function(){ mockResponse= [ { "AccountID": "xyz", .... } ] req1= { user:{ id:"" }, }, res1= { json:sinon.spy()//Is this correct } it('should call getActivatedAccounts and always this mock response',function(){ var getAccountsStub

How to unit-test an object that contain a type union using typescript, karma and sinon?

心已入冬 提交于 2019-12-13 02:47:33
问题 I'm doing unit-testing for a project which is written in typescript with angular framework, by applying karma with mocha and chai frameworks. And there's an interface for the object as: interface ISolution { _id: string; paymentFrequency: PaymentFrequency; }; And PaymentFrequency type is defined as: type PaymentFrequency = 'MONTHLY' | 'ANNUAL' | 'SINGLE'; In the controller, open(solution: ISolution) { }; The problem is when I tried to mock the solution as: let solution = { _id: 'aa0',

karma-sinon-chai fails after moving to Windows

那年仲夏 提交于 2019-12-11 20:36:14
问题 I have unit tests running under Karma, and am using the sinon-chai library. Everything was working fine on Mac, but now I have moved to Windows the following error is occuring: C:\Users\mchambe4\dev\simple\client>node ./node_modules/gulp/bin/gulp.js unit-tests-dev [16:29:31] Using gulpfile ~\dev\simple\client\gulpfile.js [16:29:31] Starting 'unit-tests-dev'... [16:29:31] Starting Karma server... WARN [karma]: Port 9876 in use INFO [karma]: Karma v0.12.37 server started at http://localhost

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

工具导航Map