sinon

Mocking/Stubbing `super` calls

こ雲淡風輕ζ 提交于 2019-12-22 06:38:04
问题 I would like to mock out super calls, especially constructors in some ES6 classes. For example import Bar from 'bar'; class Foo extends Bar { constructor(opts) { ... super(opts); } someFunc() { super.someFunc('asdf'); } } And then in my test, I would like to do something like import Foo from '../lib/foo'; import Bar from 'bar'; describe('constructor', function() { it('should call super', function() { let opts = Symbol('opts'); let constructorStub = sinon.stub(Bar, 'constructor'); new Foo(opts

How to load a JS file that is not a module in dojo?

痴心易碎 提交于 2019-12-22 06:09:11
问题 I'll start by saying that I am a javascript and dojo noob. However, I have been working on writing some unit tests for my js code, using the D.O.H framework. One thing I noticed is that the framework does not seem to have a way to mock XHR requests. So I decided to use sinon for the mocking. Heres my question, I cannot successfully load the sinon code into my dojo module. Here is what I tried: define(["doh/runner", "tests/sinon-1.4.2"], function(doh, sinnon) { ... }); I have the tests package

correct usage of sinon's fake XMLHttpRequest

女生的网名这么多〃 提交于 2019-12-22 05:58:09
问题 I am creating XMLHttpRequest javascript module to get JSON data from server. Here is the code: (function() { var makeRequest = function(url,callback,opt) { var xhr; if (XMLHttpRequest) { // Mozilla, Safari, ... xhr = new XMLHttpRequest(); } else if (ActiveXObject) { // IE try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!xhr) { callback.call(this, 'Giving up :( Cannot create an XMLHTTP instance', null);

correct usage of sinon's fake XMLHttpRequest

∥☆過路亽.° 提交于 2019-12-22 05:58:01
问题 I am creating XMLHttpRequest javascript module to get JSON data from server. Here is the code: (function() { var makeRequest = function(url,callback,opt) { var xhr; if (XMLHttpRequest) { // Mozilla, Safari, ... xhr = new XMLHttpRequest(); } else if (ActiveXObject) { // IE try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!xhr) { callback.call(this, 'Giving up :( Cannot create an XMLHTTP instance', null);

Mock a class method using mockery and sinon

爱⌒轻易说出口 提交于 2019-12-22 05:49:06
问题 I'm learning to unit test using the node module mockery with sinon. Using only mockery and a plain class I'm able to inject a mock successfully. However I would like to inject a sinon stub instead of a plain class but I'm having a lot of troubles with this. The class I am trying to mock: function LdapAuth(options) {} // The function that I want to mock. LdapAuth.prototype.authenticate = function (username, password, callback) {} And here is the code I'm currently using in my beforeEach()

TypeError: 'undefined' is not a function (evaluating 'sinon.spy()')

烂漫一生 提交于 2019-12-22 02:31:23
问题 I'm trying to use sinon.js in testing of a backbone application. But unfortunately I cannot use spy method due to error: TypeError: 'undefined' is not a function (evaluating 'sinon.spy()') Here is the steps to reproduce the error: Create an empty project with backbone yeoman generator Install sinon: cd test && bower install sinon Include in test/index.html <script src="bower_components/sinon/lib/sinon.js"></script> Create spy in test/spec/test.js: describe('Give it some context', function ()

stubbing ES6 super methods using sinon

大城市里の小女人 提交于 2019-12-21 13:58:18
问题 I am having a problem stubbing the base class methods using Sinon. In the example below I am stubbing the call to base class method GetMyDetails as follows. I am sure there is a better way. actor = sinon.stub(student.__proto__.__proto__,"GetMyDetails"); And also the value of this.Role ends up being undefined. I have created a simple class in javascript "use strict"; class Actor { constructor(userName, role) { this.UserName = userName; this.Role = role; } GetMyDetails(query,projection,populate

sinon stub not replacing function.

痴心易碎 提交于 2019-12-21 09:22:10
问题 I tried a dummy module and to stub it, but does not work. the app.js function foo() { return run_func() } function run_func() { return '1' } exports._test = {foo: foo, run_func: run_func} the test.js app = require("./app.js")._test describe('test', function(){ it('test', function(){ var test_stub = sinon.stub(app, 'run_func').callsFake( function(){ return '0' }) test_stub.restore() var res = app.foo() assert.equal('0', res) }) }) I tried the advice from: sinon stub not replacing function But

Sinon - how to stub nested function?

橙三吉。 提交于 2019-12-21 05:06:41
问题 Apologies if this is a simple question, I'm relatively new to Node and Sinon. I'm struggling trying to figure out how to assert that a nested asynchronous function was called in Nodejs. I'm using mocha, chai, sinon, and request (https://github.com/request/request) but think I'm missing something basic on the stubbing part. Example inside my_app.js - var request = require('request'); function MyModule() { }; MyModule.prototype.getTicker = function(callback) { request('http://example.com/api

Test if function is called react and enzyme

我是研究僧i 提交于 2019-12-21 03:52:06
问题 I am trying to test one of the methods in my react component. It is being called after a button click so I have the simulation in place with enzyme it('clone should call handleCloneClick when clicked', () => { const cloneButton = wrapper.find('#clone-btn'); cloneButton.simulate('click'); }); My component method is here: _handleCloneClick(event) { event.preventDefault(); event.stopPropagation(); this.props.handleClone(this.props.user.id); } The _handleCloneClick is being called when the user