简体   繁体   English

如何使用Mocha测试私有功能?

[英]how to test private function using mocha?

As i am new to unit testing , I want to test my javascript code its very confusing .. i really want some ones help who could tell the solution for my problem .?? 由于我是单元测试的新手,所以我想对我的JavaScript代码进行测试,这非常令人困惑。.我真的希望有人能帮助他们解决我的问题。

if (!Notification) {
 Notification = {};
  else {
    if (typeof Notification != "object") {
      throw new Error(" already exists ");
    }
  }
 Notification.admin = function() {
    var res = {};
    var prevent = "";

    var DefaultValue = function(ans, type, commnon, status) {
      var notify = type.concat(common);
      if ($("#method").val() == "false" && type == "Text") {
        if (status) {
          return data = '<label class="checkbox-label"><input data-role="ux-checkbox" type="checkbox" disabled="disabled" data-type="' + notify + '" class="grid-checkbox">&nbsp;&nbsp</label>';
        } else {
          return data = '<label class="checkbox-label">' + '<span class="no-checkbox-label" >' + "-" + '</span>' + '</label>';
        }
      } else if (status) {
        if (ans) {
          return data = '<label class="checkbox-label"><input data-role="ux-checkbox" checked=checked type="checkbox" data-type="' + notify + '" class="grid-checkbox ">&nbsp;&nbsp</label>';
        } else {
          return data = '<label class="checkbox-label"><input data-role="ux-checkbox" type="checkbox" data-type="' + notify + '" class="grid-checkbox">&nbsp;&nbsp</label>';
        }
      } else {
        return data = '<label class="checkbox-label">' + '<span class="no-checkbox-label" >' + "-" + '</span>' + '</label>';
      }
    };
    this.init = function() {


    };}; Notification.Obj = new Notification.admin();

  $(document).ready(function() {

Notification.Obj.init();

  });

This is my private function which i want to unit test as i am using mocha and chai .. 这是我的私有功能,我想对单元进行测试,因为我正在使用mocha和chai ..

i am not able to unit test this function 我无法对该功能进行单元测试

You don't write tests for private methods. 您不会为私有方法编写测试。

Public methods are the public interface of a class. 公共方法是类的公共接口。 The ones that are called from the outside. 从外部调用的那些。 Private methods are implementation details that you don't care about. 私有方法是您不关心的实现细节。

The scope of a unit test is to the test all the public methods of a class. 单元测试的范围是测试类的所有公共方法。 (And no, making private methods public within the class is not the solution for that.) (不,在类中公开私有方法不是解决方案。)

Refactor the actual business part of your private method out into a new service. 将私有方法的实际业务部分重构为新服务。 That service will have the functionality provided through a public method. 该服务将具有通过公共方法提供的功能。 You can test that service. 您可以测试该服务。 In you current service you can then inject that service and use its functionality. 在当前服务中,您可以注入该服务并使用其功能。

That being said: You can only unit test method that don't have side-effects and whose dependency you can mock away (dependency injection). 话虽这么说:您只能单元测试方法没有副作用,并且可以嘲笑其依赖项(依赖项注入)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM