使用Sinon模拟require()' d函数

时间:2015-08-18 05:07:14

标签: javascript node.js unit-testing testing mocking

我尝试使用request-promise在测试中模拟Sinon。据我所知,Sinon嘲笑对象的方法,但request-promise只返回一个函数。有没有办法模拟一个必需的函数?

var rp = require('request-promise');
var User = require('../../models/user');

// this works
sinon.stub(User, 'message', function() {});

// This is what I'd like to do to request-promise
sinon.stub(rp, function() {});

我也看过mockrequire和proxyquire,但我认为他们都遇到了类似的问题。

2 个答案:

答案 0 :(得分:2)

以下示例应该为您解决问题。通过使用proxyquire,您可以包裹.post并避免存根.get.callmyModule.js等。我认为更清洁。

'use strict'; const rp = require('request-promise'); class MyModule { /** * Sends a post request to localhost:8080 to create a character. */ static createCharacter(data = {}) { const options = { method: 'POST', uri: 'http://localhost:8080/create-character', headers: {'content-type': 'application/json'}, body: data, }; return rp(options); } } module.exports = MyModule;

myModule.spec.js

'use strict'; const proxyquire = require('proxyquire'); const sinon = require('sinon'); const requestPromiseStub = sinon.stub(); const MyModule = proxyquire('./myModule', {'request-promise': requestPromiseStub}); describe('MyModule', () => { it('#createCharacter', (done) => { // Set the mocked response we want request-promise to respond with const responseMock = {id: 2000, firstName: 'Mickey', lastName: 'Mouse'}; requestPromiseStub.resolves(responseMock); MyModule.createCharacter({firstName: 'Mickey', lastName: 'Mouse'}) .then((response) => { // add your expects / asserts here. obviously this doesn't really // prove anything since this is the mocked response. expect(response).to.have.property('firstName', 'Mickey'); expect(response).to.have.property('lastName', 'Mouse'); expect(response).to.have.property('id').and.is.a('number'); done(); }) .catch(done); }); });

map.addSource('neighborhoods', {
  type: 'vector',
  url: 'mapbox://shortdiv.cj4u72j500fu72qplj0xcusp3-738n8'
})

map.addLayer({
    'id': 'neighborhood-bounds',
    'source': 'neighborhoods',
    'source-layer': 'chicago_neighborhoods',
    'type': 'line',
    'paint': {
      "line-color": "#ad0403",
      "line-width": 2
    }
});

答案 1 :(得分:1)

I found a (sort of) solution here

基本上,你背负a=a[,-1] head(a) Date Open High Low Close Volume 1 20110809 5.76 8.23 5.76 8.23 28062 2 20110810 9.78 9.78 8.10 8.13 9882 3 20110811 9.00 9.00 9.00 9.00 2978 4 20110812 9.70 9.70 8.90 9.60 5748 5 20110816 9.70 11.00 9.70 11.00 23100 6 20110818 10.90 11.00 10.90 10.90 319 w=xts(a[,-1],order.by=as.POSIXct(a[,1])) call也可以):

apply

它完成了工作,虽然我不喜欢到处都有// models/user.js var rp = require('request-promise'); var User = { save: function(data) { return rp.call(rp, { url: ..., data: data }); } } module.exports = User; // test/user.js var rp = require('request-promise'); var User = require('../../models/user'); describe('User', function() { it('should check that save passes data through', function() { sinon.stub(rp, 'call', function(data) { // check data here }); User.save({ foo: 'bar' }); }); }); ,所以我仍然希望找到更好的解决方案。