sinon

How does one stub promise with sinon?

放肆的年华 提交于 2019-11-28 04:38:43
I have a data service with following function function getInsureds(searchCriteria) { var deferred = $q.defer(); insuredsSearch.get(searchCriteria, function (insureds) { deferred.resolve(insureds); }, function (response) { deferred.reject(response); }); return deferred.promise; } I want to test following function: function search () { dataService .getInsureds(vm.searchCriteria) .then(function (response) { vm.searchCompleted = true; if (response.insureds.length > 100) { vm.searchResults = response.insureds.slice(0, 99); } else { vm.searchResults = response.insureds; } }); } How would I stub the

Cleaning up sinon stubs easily

久未见 提交于 2019-11-28 03:25:11
Is there a way to easily reset all sinon spys mocks and stubs that will work cleanly with mocha's beforeEach blocks. I see sandboxing is an option but I do not see how you can use a sandbox for this beforeEach -> sinon.stub some, 'method' sinon.stub some, 'mother' afterEach -> # I want to avoid these lines some.method.restore() some.other.restore() it 'should call a some method and not other', -> some.method() assert.called some.method keithjgrant Sinon provides this functionality through the use of Sandboxes , which can be used a couple ways: // manually create and restore the sandbox var

How to properly unit test jQuery's .ajax() promises using Jasmine and/or Sinon?

ε祈祈猫儿з 提交于 2019-11-28 03:03:48
I've got a fairly straightforward function which returns a jQuery .ajax() promise as such: CLAW.controls.validateLocation = function(val, $inputEl) { return $.ajax({ url: locationServiceUrl + 'ValidateLocation/', data: { 'locationName': val }, beforeSend: function() { $inputEl.addClass('busy'); } }).done(function(result) { // some success clauses }).fail(function(result) { // some failure clauses }).always(function() { // some always clauses }); } For the most part, this new promises interface works like a dream, and eliminating callback pyramids when using jQuery's .ajax() is great. However,

How do I use Sinon with Typescript?

不羁岁月 提交于 2019-11-27 23:04:35
问题 If I use sinon with typescript then how do I cast the sinon mock to an instance of my object? For instance a SinonMock would be returned but my controller under test may require a specific service passed in to its constructor. var myServiceMock: MyStuff.MyService = <MyStuff.MyService (sinon.mock(MyStuff.MyService)); controllerUnderTest = new MyStuff.MyController(myServiceMock, $log); Can sinon be used with Typescript? 回答1: You may need to use an <any> type assertion to make the type wide

Sinon stub function used with destructuring

江枫思渺然 提交于 2019-11-27 22:29:34
I wish to stub a function used in the file I'm currently testing. This function is required with a destructuring like this: const { theFunctionIWant } = require('path/to/module') When testing, the stub is never called, and the real function proceed to be called. But when I require it 'normally' (i.e: without destructuring) const myModule = require('path/to/module') then the stub is correctly used and everything works fine I sense that it's because of how the destructuring works and the fact that sinon stub the object property and not the function directly. Anyhow if you can provide me some

Test that a function calls another function in an ES6 module with Sinon.js

倖福魔咒の 提交于 2019-11-27 21:50:31
问题 I want to test that a function in an ES6 module calls another function using Sinon.js. Here's the basic layout of what I'm doing: foo.js export function bar() { baz(); } export function baz() { ... } test.js import sinon from 'sinon'; import * as Foo from '.../foo'; describe('bar', function() { it('should call baz', function() { let spy = sinon.spy(Foo, 'baz'); spy.callCount.should.eql(0); Foo.bar(); spy.calledOnce.should.eql(true); }); }); But the spy does not pick up the call to baz() . Is

Stub out a jQuery selector call?

核能气质少年 提交于 2019-11-27 21:09:39
问题 I'm trying to get better at unit testing my JavaScript. I have the following code: var categoryVal = $('#category').val(); if (categoryVal === '') { doSomething(); } My test runner doesn't have the #category input on the page, so how would I stub/mock out the jQuery selector here? I've looked at both the jasmin and sinon documentation, but can't figure out how to get them to work here, since their stubs operate on objects, which $ is not. 回答1: The problem here is that $() is a function that

Verifying function call and inspecting arguments using sinon spies

随声附和 提交于 2019-11-27 17:17:25
问题 I would like to verify that bar() is called inside foo() from my unit test. I figured that Sinon spies might be suitable, but I don't know how to use them. Is there any way to check that the method is called? Perhaps even extracting the arguments used in the bar() call? var spy = sinon.spy(foo); function foo(){ bar(1,2,3); } function bar(){ } foo(); // what to do with the spy? http://jsfiddle.net/8by9jg07/ 回答1: In your case, you are trying to see if bar was called, so you want to spy bar

Stubbing a Mongoose model using Sinon

吃可爱长大的小学妹 提交于 2019-11-27 16:47:40
问题 I am trying to stub the mongoose dependency used in this object: var Page = function(db) { var mongoose = db || require('mongoose'); if(!this instanceof Page) { return new Page(db); } function save(params) { var PageSchema = mongoose.model('Page'); var pageModel = new PageSchema({ ... }); pageModel.save(); } Page.prototype.save = save; } module.exports = Page; Using the answer for this question, I've tried doing this: mongoose = require 'mongoose' sinon.stub mongoose.Model, 'save' But I got

Stubbing a Mongoose model with Sinon

只谈情不闲聊 提交于 2019-11-27 14:37:48
I want to create a stub for the Mongoose save method in a particular model, so that any instance of my model I create will call the stub instead of the normal Mongoose save method. My understanding is that the only way to do this is to stub the entire model like this: var stub = sinon.stub(myModel.prototype); Unfortunately, this line of code causes my tests to throw the following error: TypeError: Cannot read property 'states' of undefined Does anyone know what is going wrong here? There are two ways to accomplish this. The first is var mongoose = require('mongoose'); var myStub = sinon.stub