sinon

Sinon not stubbing on module.exports

混江龙づ霸主 提交于 2019-11-30 19:44:44
问题 If create an file with the following contents const validateEmail = email => { sendEmail(email); }; const sendEmail = email => { return true; }; module.exports = { validateEmail, sendEmail, }; And a test that tries to stub out the second function... it('Should call sendEmail if a valid email is passed', () => { let sendEmailSpy = sinon.stub(checkEmail, 'sendEmail'); checkEmail.validateEmail('acorrectemail@therightformat.com'); assert.isTrue(sendEmailSpy.called); }); It still calls the

Creating request stub with sinon in mocha

蹲街弑〆低调 提交于 2019-11-30 17:07:50
I'm using mocha to test some classes and I need to create a stub of request library. I'm using sinon , and I'm able to create a stub of the request.get method but I'm not able to create a stub of the request method (the http calls try to connect to a server). As I have read, request.get is an alias for request but when I stub request.get it has no effect over request calls. This code works (using request.get ): In tests: request = require 'request' describe "User test", -> user = {} before (done) -> user = new test.user('Ander', 18) sinon.stub(request, 'get').yields(null, {statusCode: 200},

Stub save Instance Method of Mongoose Model With Sinon

瘦欲@ 提交于 2019-11-30 16:51:52
问题 I am trying to test a service function I use to save a widget using a Mongoose model. I want to stub out the save instance method on my model, but I cannot figure out a good solution. I have seen other suggestions, but none seem to be complete. See... this, and this. Here is my model... // widget.js var mongoose = require('mongoose'); var widgetSchema = mongoose.Schema({ title: {type: String, default: ''} }); var Widget = mongoose.model('Widget', widgetSchema); module.exports = Widget; Here

backbone.js click event spy is not getting called using jasmine.js and sinon.js

六月ゝ 毕业季﹏ 提交于 2019-11-30 12:36:55
问题 I am trying to test a button click using backbone.js, jasmine.js and sinon.js. But the following test case fails. I am using a spy to track whether it is getting called or not. Can you please help me with this? Thanks. New Task Template <script id='new_task_template' type='text/template'> <input type='text' id='new_task_name' name='new_task_name'></input> <button type='button' id='add_new_task' name='add_new_task'>Add Task</button> </script> NewTaskView T.views.NewTaskView = Backbone.View

Mocking JavaScript constructor with Sinon.JS

核能气质少年 提交于 2019-11-30 10:57:39
I'd like to unit test the following ES6 class: // service.js const InternalService = require('internal-service'); class Service { constructor(args) { this.internalService = new InternalService(args); } getData(args) { let events = this.internalService.getEvents(args); let data = getDataFromEvents(events); return data; } } function getDataFromEvents(events) {...} module.exports = Service; How do I mock constructor with Sinon.JS in order to mock getEvents of internalService to test getData ? I looked at Javascript: Mocking Constructor using Sinon but wasn't able to extract a solution. // test.js

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

跟風遠走 提交于 2019-11-30 08:56: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

Stubbing window functions in Jest

三世轮回 提交于 2019-11-30 08:34:22
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? 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. 来源: https://stackoverflow.com/questions/41732903/stubbing-window-functions-in-jest

Sinon does not seem to spy for an event handler callback

折月煮酒 提交于 2019-11-30 05:23:18
问题 I'm testing a backbone view with Jasmin, Simon and jasmin-simon. Here is the code: var MessageContainerView = Backbone.View.extend({ id: 'messages', initialize: function() { this.collection.bind('add', this.addMessage, this); }, render: function( event ) { this.collection.each(this.addMessage); return this; }, addMessage: function( message ) { console.log('addMessage called', message); var view = new MessageView({model: message}); $('#' + this.id).append(view.render().el); } }); Actually, all

Mocking JavaScript constructor with Sinon.JS

扶醉桌前 提交于 2019-11-29 16:14:35
问题 I'd like to unit test the following ES6 class: // service.js const InternalService = require('internal-service'); class Service { constructor(args) { this.internalService = new InternalService(args); } getData(args) { let events = this.internalService.getEvents(args); let data = getDataFromEvents(events); return data; } } function getDataFromEvents(events) {...} module.exports = Service; How do I mock constructor with Sinon.JS in order to mock getEvents of internalService to test getData ? I

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

帅比萌擦擦* 提交于 2019-11-29 12:44:46
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('sinon'); var proxyquire= require('proxyquire'); I get the below error Error: Cannot find module '.