在单元测试显示模块时如何对私有功能进行存根 [英] How do you stub private functions when unit testing a revealing module

查看:71
本文介绍了在单元测试显示模块时如何对私有功能进行存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在构建一个节点模块,该模块包装对GitHub API的大量调用,并且以我的无限智慧,已使用显示模块模式构建了该模块,使包装函数保持私有状态,并且仅公开了简单的方法.请参见下面的示例:

I have been building a node module that wraps a number of calls to the GitHub API and in my infinite wisdom have built this using the revealing module pattern, keeping my wrapping functions private and only exposing simple methods. See the example below:

github.shortcuts = (function(){

  var appPath;

  var createRepo = function(name){
    var deferred = Q.defer();
    github.repos.create({
      name: name,
      auto_init: true
    }, function(error, result){
      if (error) {
        deferred.reject(new Error(error));
      } else {
        deferred.resolve(result);
      }
    });
    return deferred.promise;
  };

  var updateRef = function(result){
    var deferred = Q.defer();
    var user = result.user;
    var repo = result.repo;
    github.gitdata.updateReference({
      user: user,
      repo: repo,
      ref: 'heads/master',
      sha: result.sha
    }, function(error, result){
      if (error) {
        deferred.reject(new Error(error));
      } else {
        deferred.resolve(result);
      }
    });
    return deferred.promise;
  };

  return {
    init: function(token, name, path){
      var deferred = Q.defer();
      appPath = path;

      var error = function(error){
        return deferred.reject(error);
      };

      github.authenticate({
        type: "oauth",
        token: token
      });

      createRepo(name)
        .then(updateRef, error)
        .then(function(result){
          deferred.resolve(result);
        }, error);

      return deferred.promise;
    }
  };

})();

但是为此编写单元测试却导致了我的问题.我不想测试我的私有函数,而只是测试公共的init(),但是我想对私有函数存根,以便测试不会调用GitHub API.我正在使用Mocha和Chai进行测试,并使用Sinon进行间谍/存根测试.

However writing unit tests for this is causing me issues. I don't want to test my private functions, just the public one init(), however I would want to stub the private functions so that the test will not call to the GitHub API. I am using Mocha and Chai for my tests and Sinon for my spys/stubs.

有关如何对这些功能进行存根的任何建议,或者如果这是一个错误的模式,那么我应该如何测试该模块,将不胜感激!

Any advice on how to stub these functions or if this is a bad pattern, how else I should test this module would be appreciated!

推荐答案

您应该隔离类中有问题的部分并对其进行模拟,而不是对私有方法进行存根. IMO是一种设计异味,它需要模拟或存根私有方法,这表明该类太大,您应将各种问题分开.

You should isolate the problematic part of the class and mock that out, rather than stubbing private methods. The need to mock or stub a private method is a design smell, IMO, indicating that the class is too large and you should separate the various concerns.

在这种情况下,您可以将github API作为参数传递给init,而不是挖掘类的内部结构,而只是提供一个返回静态结果的伪API.

In this case, you could pass the github API as a parameter to init and, rather than digging through the internals of your class, simply provide a fake API that returns static results.

这在您开始测试错误案例时特别有用,因为您的模拟API会抛出适当的错误,并仅允许您测试错误处理行为.

This is especially helpful when you start testing error cases, as your mock API can throw the appropriate error and allow you to only test the error handling behavior.

这篇关于在单元测试显示模块时如何对私有功能进行存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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