如何测试函数是否调用特定方法/函数? [英] How do I test if a function calls a specific method/function?

查看:156
本文介绍了如何测试函数是否调用特定方法/函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mocha有没有办法测试某个函数是否调用特定方法或外部函数?

Is there a way in Mocha to test if a function calls a specific method or external function?

我正在使用Mocha和Chai,但是我打开了任何其他断言库。

好的,所以测试是否正在调用methid非常容易使用兴农。我不确定测试是否正在调用外部函数。所以我更新了这些例子来代表一些更现实世界的东西。我正在开发一个节点应用,所以 foo.js bar.js 都是模块。

Ok, so testing whether a methid is being called is pretty easy using sinon. I'm not sure about testing to see if an external function is being called though. So I updated the examples to represent something a little more "real world". I am working on a node app, so foo.js and bar.js are both modules.

var bar = require('bar');
var xyz = function () {};

var Foo = module.exports = function () {
  this.bar();
  bar();
  xyz();
};
Foo.prototype.bar = function () {};



bar.js



bar.js

var bar = module.exports = function () {};



fooSpec.js



fooSpec.js

var chai      = require('chai');
var sinon     = require('sinon');
var sinonChai = require('sinonChai');
var expect    = chai.expect;
var Foo       = require('../lib/foo');

chai.use('sinonChai');

describe('Foo', function () {

  var method;

  beforeEach(function (done) {
    method = sinon.spy(Foo.prototype, 'bar');
    done();
  });
  afterEach(function (done) {
    method.restore();
    done();
  });

  it('should call Foo.prototype.bar() immediately', function () {

    new Foo();
    expect(method).to.have.been.called;

  });

  it('should call the module bar immediately', function () {
    // ????????????
  });

  it('should call xyz() immediately', function () {
    // ????????????
  });
});

所以你可以看到我已经找到了如何测试 Foo .prototype.bar ,但我找不到实现第二次和第三次测试的方法。

So as you can see I've figured out how to test for Foo.prototype.bar, but I can't find a way to implement the second and third tests.

推荐答案

所以这个问题真的是二合一。

So this question was really two in one.

首先,如何测试方法是否被调用:
我在示例中列出了这个代码,但基本上,使用sinon.js你只需将方法包装在一个间谍中,它允许你编写一个期望调用spy的测试。

Firstly, "how to test if a method is being called": I laid out the code for this in the example, but basically, using sinon.js you just wrap the method in a "spy" which allows you to write a test that expects that spy to have been called.

其次,如何测试私有函数(未作为模块的一部分导出的函数)是否已被调用:

Secondly, "how to test if a private function(one that was not exported as part of the module) has been called":

基本上,你没有。可以在测试环境中而不是在生产环境中导出这些功能,但这对我来说似乎有些太过分了。

Basically, you don't. It is possible to export these functions when in a testing environment and not in production, but this seems a little too hacky to me.

我得出的结论是当调用另一个模块时,你应该打破TDD周期而不是测试它,因为它可能只是少量的代码而且模块已经经过测试了。

I've come to the conclusion that when calling another module you should just break the TDD cycle and not test for this since it's probably going to be a small amount of code and the module will have already been tested on it's own.

如果您正在调用在您的模块中声明的私有函数并想要测试它,您应该编写一个更广泛的测试来测试调用此函数的结果,而不是测试函数是否被调用或者函数中实际发生了什么。

If you are calling a private function that is declared within you're module and want to test it you should write a more broad test that tests for the result of this function being called rather than testing whether the function is being called or what is actually happening within the function.

这是一个非常简单的例子:

Here's a really simple example:

var _ = require('lodash');

var Foo = module.exports = function (config) {

  this.config = _.merge({
      role: 'user',
      x: '123',
      y: '321'
    },
    config);

  this.config.role = validateRole(this.config.role);
};

var validateRole = function (role) {
  var roles = [
    'user', 'editor', 'admin'
  ];

  if (_.contains(roles, role)) {
    return role;
  } else {
    return 'user'
  }
};



fooSpec.js



fooSpec.js

var chai = require('chai');
var expect = chai.expect;
var Foo = require('../lib/foo');

describe('Foo', function () {

  it('should set role to \'user\' if role is not valid', function () {

    var foo = new Foo({role: 'invalid'});
    expect(foo.config.role).to.equal('user');

  });

};

这篇关于如何测试函数是否调用特定方法/函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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