sinon

Stubbing route callback render route status not found

♀尐吖头ヾ 提交于 2020-01-05 04:19:33
问题 I have a route app.get("/test", myControllers.controllerTest) The controllerTest returns current time as result to query my route test file describe.("GET /test", () => { it("should return correct response", done => { sinon.stub(myControllers, "controllerTest").yields(null, {time:'fake-time'}); app = require("../../server"); chai .request(app) .get("/test") .then(res => { expect(res.status).to.equal(200); expect(myControllers.controllerTest).to.have.been.calledOnce; done(); }) .catch(err=>{

Unit test express route calls controller method?

元气小坏坏 提交于 2020-01-04 16:59:11
问题 I see some similar questions, but my setup is slightly different and I can't figure out a good way to test this. I'm trying to test that my express app routes are directed to the correct controller methods. For example - //server.js, base application var express = require("express"); var app = express(); require("./routes.js")(app); ... //routes.js var menuController = require("./controllers/menu.js"); module.exports = function(expressApp) { expressApp.get('/menu', menuController.getMenu); };

Testing failed requests in node

做~自己de王妃 提交于 2020-01-04 07:00:31
问题 I have some code that looks like the following: var request = require('request'); function Service(){ this._config = require('../path/to/config.json'); } Service.prototype.doThing = function(){ return new Promise(function(resolve, reject){ request.post(url, {payload}, function(error, response, body){ //handle response resolve(true); }).on('error', function(err){ //handle errors resolve(false); }); }); } I'm trying to test the block that runs on error, but having difficulty because of the

Testing: how to assert between two sequential promises/run.laters? How to skip `run.later` waiting in tests?

会有一股神秘感。 提交于 2020-01-03 18:17:32
问题 Here's a simple component: App.FooBarComponent = Ember.Component.extend({ tagName: "button", status: "Ready", revertStatusPeriodMs: 2000, click: function() { this.set('status', 'Pending'); // A fake ajax this.run() .then( function() { this.updateStatus('Finished'); }.bind(this)) .catch( function() { this.updateStatus('Error'); }.bind(this)); }, run: function() { return new Ember.RSVP.Promise( function(resolve) { Ember.run.later( function() { resolve(); }, 500); }); }, updateStatus: function

Testing: how to assert between two sequential promises/run.laters? How to skip `run.later` waiting in tests?

自作多情 提交于 2020-01-03 18:16:02
问题 Here's a simple component: App.FooBarComponent = Ember.Component.extend({ tagName: "button", status: "Ready", revertStatusPeriodMs: 2000, click: function() { this.set('status', 'Pending'); // A fake ajax this.run() .then( function() { this.updateStatus('Finished'); }.bind(this)) .catch( function() { this.updateStatus('Error'); }.bind(this)); }, run: function() { return new Ember.RSVP.Promise( function(resolve) { Ember.run.later( function() { resolve(); }, 500); }); }, updateStatus: function

TypeError: Attempted to wrap undefined property as function

不羁的心 提交于 2020-01-03 17:05:49
问题 The example below is simplified. I have a getter method: class MyClass { constructor() {} get myMethod() { return true; } } which is processed by babel. And I want to mock it like this: var sinon = require('sinon'); var MyClass = require('./MyClass'); var cls = new MyClass(); var stub = sinon.stub(cls, 'myMethod'); stub.returns(function() { return false; }); But I get the following error: TypeError: Attempted to wrap undefined property myMethod as function And this happens on both version 1

How to stub Backbone View instantiated within another Views render method when Using RequireJS (and Jasmine / Sinon)

Deadly 提交于 2020-01-03 16:48:35
问题 I am attempting to write unit tests using Jasmine and Sion but I am struggling to find the equivalent of the following when using RequireJs to load modules: sinon.stub(window, "MyItemView"); When using RequireJs, I am unable to stub this way as MyItemView is not attached to the window. The following is an example of when I need stub the MyItemView: var MyView = Backbone.View.extend({ el: '#myElement', initialize : function() { var that = this; this.collection.fetch({ success : function() {

How to spy on a default exported function

不羁岁月 提交于 2020-01-03 07:22:12
问题 sinon.spy takes 2 parameters, object and function name. I have a module as listed below: module.exports = function xyz() { } How do I define a spy for xyz ? I don't have object name to use. Thoughts? 回答1: The above actually doesn't work if you're using the ES6 modules import functionality, If you are I've discovered you can actually spy on the defaults like so. // your file export default function () {console.log('something here');} // your test import * as someFunction from './someFunction';

node js unit testing: mocking require dependency

倖福魔咒の 提交于 2020-01-02 02:47:05
问题 I am having issues writing unit test for the following setup as a jira.js file (in a node.js module): var rest = require('restler'); // https://www.npmjs.com/package/restler module.exports = function (conf) { var exported = {}; exported.getIssue = function (issueId, done) { ... rest.get(uri).on('complete', function(data, response) { ... }; return exported; }; Now, i want to write unit test for my getIssue function. 'restler' is a REST client through which i make REST calls to the JIRA API to

Stub out module function

余生长醉 提交于 2020-01-01 04:41:10
问题 Edit: Being a little bit more precise. I want to test usecases for a Github API wrapper extension, that our team has created. For testing, we don't want to use API wrapper extension directly, so we want to stub out its functions. All calls to the API wrapper should be stubbed out for the tests, not just creating a clone stub. I have a module "github" in Node.js: module.exports = function(args, done) { ... } And I am requiring it like this: var github = require('../services/github'); Now, I