Sinon-如何对嵌套函数进行存根? [英] Sinon - how to stub nested function?

查看:116
本文介绍了Sinon-如何对嵌套函数进行存根?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果这是一个简单的问题,那么我对Node和Sinon还是比较陌生的.我正在努力弄清楚如何断言在Node.js中调用了嵌套的异步函数.

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.

我正在使用摩卡咖啡,柴,西农和请求( https://github.com/request/request ),但我想我在存根部分缺少一些基本知识.

I'm using mocha, chai, sinon, and request (https://github.com/request/request) but think I'm missing something basic on the stubbing part.

my_app.js中的示例-

Example inside my_app.js -

var request = require('request');

function MyModule() {
};

MyModule.prototype.getTicker = function(callback) {
    request('http://example.com/api/ticker', function(error, response) {
        if (error) {
            callback(error);
        } else {
            callback(null, response);
        }
    });
};

exports.mymodule = new MyModule();

在测试内部.我正在尝试取消对请求的调用,并提供一些虚拟数据以返回.但是我在创建存根的行上不断收到未定义请求"错误.

Inside the test. I'm trying to stub out the call to request and provide some dummy data to return. But I keep getting an error "request is not defined" on the line where I"m creating the stub.

var myApp = require('../my_app.js')
    ,assert = require("assert")
    ,chai = require('chai')
    ,sinon = require('sinon')
    ,expect = chai.expect;

describe('mymodule object', function() {

    var mymodule = myApp.mymodule;

    before(function(done) {
        sinon.stub(request).yields(null, JSON.stringify({
            price: '100 USD'
        }));
        done();
    });

    it('getTicker function should call request on example ticker', function(done) {
        mymodule.getTicker(function(error, result){
            request.called.should.be.equal(true);
            done();
        });
    });

});

我知道我可以分配sinon.stub(objname,"funcname")或sinon.stub("funcname"),但是那些仅设置外部对象,我试图对函数内部的函数请求进行存根getTicker.

I know I can assign sinon.stub(objname, "funcname") or sinon.stub("funcname"), but those only set the outer object , I'm trying to stub the function request which is inside the function getTicker.

关于如何执行此操作的任何想法?也许我也需要使用间谍(但是怎么做?)还是有更好的方法来测试上面的getTicker函数?

Any ideas on how to do this? Maybe I need to use a spy as well (but how?) Or is there a better approach to test the above getTicker function?

推荐答案

由于在测试范围内request变量未知,因此您将收到未定义的消息.但是,即使您要更正此问题并将request库分配给该变量,您仍然会收到错误消息,因为sinon要求在提供的任何对象上都有一个方法才能创建存根.

You are receiving the undefined message because the request variable is unknown within the scope of your test. However, even if you were to correct this and assign the request library to the variable, you would still receive an error as sinon requires a method on any provided object in order to create a stub.

这样的结果是request函数本身不能存根,因为它不存在于对象上,而是作为定义了其他方法的函数.因此,为了支持可测试性,最好不要在代码中直接使用request,而应使用随后可以进行存根的附加方法.例如:

The consequence of such is that the request function itself cannot be stubbed as it does not exist on an object but as a function onto which other methods are defined. In order to support testability, therefore, it'd be preferable to forgo using request directly in your code, and instead use its attached methods which can then be stubbed. For example:

my_app.js

MyModule.prototype.getTicker = function(callback) {
  request.get('http://example.com/api/ticker', function(error, response) {
    ...
  });
};

my_test.js

var request = require('request');

before(function() {
  sinon.stub(request, 'get').yields(null, JSON.stringify({price: '100 USD'}));
});

it('getTicker function should call request on example ticker', function() {
  mymodule.getTicker();
  request.get.called.should.be.equal(true);
});

(请注意,当存根同步时,不需要异步运行mocha).

(Note that running mocha asynchronously is un-necessary when a stub is synchronous).

这篇关于Sinon-如何对嵌套函数进行存根?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆