sinon

Stubbing window functions in Jest

不想你离开。 提交于 2019-11-29 12:27:50
问题 In my code, I trigger a callback upon "OK" click of a window.confirm prompt, and I want to test that the callback is triggered. In sinon , I can stub the window.confirm function via: const confirmStub = sinon.stub(window, 'confirm'); confirmStub.returns(true); Is there a way I can achieve this stubbing in Jest? 回答1: In jest you can just overwrite them using global . global.confirm = () => true As in jest every test file run in its own process you don't have to reset the settings. 回答2: I just

undefined|0|ReferenceError: Strict mode forbids implicit creation of global property 'csrf_token'

可紊 提交于 2019-11-29 09:24:38
So, this was quite an interesting problem I have been running into. I am currently building a backbone.js - Rails app. Generally just building this for learning purposes. I am (like any good rails dev) doing my best at TDD/BDD and I ran into a problem with capybara. I have an integration spec that merely tests root_path works (Backbone history starts, displays initial information, etc...). require 'spec_helper' describe "RentalProperties", js: true do describe "GET /" do it "should show a list of properties" do visit root_path eventually{page.should have_content("Something")} end end end I am

How do I use Sinon with Typescript?

喜欢而已 提交于 2019-11-29 05:30:31
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? You may need to use an <any> type assertion to make the type wide before you narrow it to your specific type: var myServiceMock: MyStuff.MyService = <MyStuff.MyService> <any>

QUnit, Sinon.js & Backbone unit test frustration: sinon spy appears to fail to detect Backbone Model event callbacks

老子叫甜甜 提交于 2019-11-29 03:50:17
问题 In the following unit test code: TestModel = Backbone.Model.extend({ defaults: { 'selection': null }, initialize: function() { this.on('change:selection', this.doSomething); }, doSomething: function() { console.log("Something has been done."); } }); module("Test", { setup: function() { this.testModel = new TestModel(); } }); test("intra-model event bindings", function() { this.spy(this.testModel, 'doSomething'); ok(!this.testModel.doSomething.called); this.testModel.doSomething(); ok(this

Verifying function call and inspecting arguments using sinon spies

限于喜欢 提交于 2019-11-29 03:02:43
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/ In your case, you are trying to see if bar was called, so you want to spy bar rather than foo . As described in the doc : function bar(x,y) { console.debug(x, y); } function foo(z) { bar(z,

Stubbing a Mongoose model using Sinon

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 02:26:58
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 the error: TypeError: Attempted to wrap undefined property save as function I also tried this: sinon

Stub out a jQuery selector call?

放肆的年华 提交于 2019-11-28 23:05:08
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. Andreas Köberle The problem here is that $() is a function that returns an object with the method val() . So you have to stub $() to return a stubbed object

Sinon function stubbing: How to call “own” function inside module

假装没事ソ 提交于 2019-11-28 11:20:42
I am writing some unit tests for node.js code and I use Sinon to stub function calls via var myFunction = sinon.stub(nodeModule, 'myFunction'); myFunction.returns('mock answer'); The nodeModule would look like this module.exports = { myFunction: myFunction, anotherF: anotherF } function myFunction() { } function anotherF() { myFunction(); } Mocking works obviously for use cases like nodeModule.myFunction() , but I am wondering how can I mock the myFunction() call inside anotherF() when called with nodeModule.anotherF() ? You can refactor your module a little. Like this. var service = {

How to stub/mock submodules of a require of nodejs using sinon

为君一笑 提交于 2019-11-28 06:09:11
问题 I am using sinon as for unit testing a nodejs(Hapijs) functionality. This function is in index.js. I am include the index.js in my test file as var index=require('./index.js'); But again inside the index.js there is require var library= require('./library.js') Again the library.js has the require of a third part functionality var googlelib=require('googlelib') Now when I run my testfile testfunc.js below var index= require('./index.js'); var assert = require('assert'); var sinon = require(

Mocking Redis Constructor with Sinon

六月ゝ 毕业季﹏ 提交于 2019-11-28 05:36:42
问题 I'm trying to figure out a way to mock redis in this module: const Redis = require('ioredis'); const myFunction = { exists: (thingToCheck) { let redis_client = new Redis( 6379, process.env.REDIS_URL, { connectTimeout: 75, dropBufferSupport: true, retryStrategy: functionHere }); redis_client.exists(thingToCheck, function (err, resp) { // handlings in here }); } }; Using this test-code: const LambdaTester = require('lambda-tester'); const chai = require('chai'); const expect = chai.expect;