摩卡&诗乃方法间谍

时间:2015-02-01 16:41:11

标签: javascript node.js mocha sinon

我正在为我的应用程序设置测试,我希望检查一个方法被使用Sinon调用x次,我的测试框架是Mocha。

我如何实现这一目标,下面是我想测试的代码,我希望确保recursiveRequest被createClients调用x次。

Nodezilla.prototype.createClients = function(batched, i){
    var self = this;

    if(batched)
        batchRequests();

    if(batched == false && i < this.virtualUsers){
        // set timeout to help prevent out of memory errors
        setTimeout( function() {
            self.createClients(false, i+1);
        }, 0 );  
    }else{
        this.recursiveRequest();
    }
};

Nodezilla.prototype.recursiveRequest = function(){
    var self    =   this;
    self.hrtime =   process.hrtime();

    if(!this.halt){
        self.reqMade++;

        this.http.get(this.options, function(resp){
            resp.on('data', function(){})
                .on("end", function(){
                    self.onSuccess();
                });
        }).on("error", function(e){
            self.onError();
        });
    }
};

尝试测试但无效,因为callCount返回0。

var assert      = require('assert'),
    sinon       = require('sinon'),
    nz          = require('../Nodezilla'),
    nt          = new nz("localhost", 10);

describe('Nodezilla', function(){
  describe('createClients', function(){
    before(function() {
        sinon.spy(nt, "recursiveRequest");
    });

    it('should call recursiveRequest() 10 times', function(){
        nt.createClients(false, 0);

        assert(nt.recursiveRequest.callCount);

    });
  });
});

1 个答案:

答案 0 :(得分:2)

createClients似乎是一个异步请求,没有回调/保证。 这意味着您的测试会立即进行评估,而不会等待结果。 我建议用回调或承诺重新编写函数,这样你就可以对处理完成的事件采取行动,然后这应该有效:

var assert = require('assert'),
    sinon  = require('sinon'),
    nz     = require('../Nodezilla'),
    nt     = new nz("localhost", 1);

describe('Nodezilla', function () {
    describe('createClients', function () {
        it('should call recursiveRequest() 10 times', function (itCallback) {
            // Moved into the test:                
            sinon.spy(nt, "recursiveRequest");
            nt.createClients(false, 0, function(){
                // Needs to wait for action to actually be called:
                assert(nt.recursiveRequest.callCount == 10);
                // Now that the test is actually finished, end it:
                itCallback();
            });
        });
    });
});

跳过before语句,因为这可能会影响范围,可以在测试中调用sinon.spy同步。

另请注意,我在此声明中引入了一个回调: it('should call recursiveRequest() 10 times', function (callback) { 在调用内部回调之前保持测试结束。

编辑: 至于添加回调,我不确定batchRequests()做了什么,但是那样:

Nodezilla.prototype.createClients = function (batched, i, cb) {
    var self = this;

    if (batched)
        batchRequests();

    if (batched == false && i < this.virtualUsers) {
        // set timeout to help prevent out of memory errors
        setTimeout(function () {
            self.createClients(false, i + 1, cb);
        }, 0);
    } else {
        this.recursiveRequest(cb);
    }
};

然后:

Nodezilla.prototype.recursiveRequest = function (cb) {
    var self = this;
    self.hrtime = process.hrtime();

    if (!this.halt) {
        self.reqMade++;

        this.http.get(this.options, function (resp) {
            resp.on('data', function () {
            })
                .on("end", function () {
                    self.onSuccess();
                    cb();
                });
        }).on("error", function (e) {
            self.onError();
            cb();
        });
    } else {
        // I assume you are doing nothing in this case, so just call the callback:
        cb();
    }
};

另请注意,您可以使用回调进行错误处理。