私有方法 使用 Jasmine 进行单元测试 [英] Private method Unit testing with Jasmine

查看:21
本文介绍了私有方法 使用 Jasmine 进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jasmine 为 Angular 应用程序编写测试用例.但是许多内部方法在服务中被声明为私有.

I was coding test cases for an angular application using jasmine. But many internal methods are declared as private in the services.

例子:

App.service('productDisplay', function(){
    var myPrivate = function(){
        //do sth
    }
    this.doOfferCal = function(product, date){
        //call myPrivate
        //do sth too
        return offer;
    }
});

使用 jasmine 可以直接为doOfferCal"编写测试代码,但我也想为 myPrivate 编写单元测试.

Using jasmine it straightforward to code test for "doOfferCal" but I want to write unit test for myPrivate too.

我该怎么做?

提前致谢.

推荐答案

谢谢jabko87.

此外,如果您想传递参数,请使用以下示例:

In addition, if you want to pass the the arguments use the below example:

const myPrivateSpy = spyOn<any>(service, 'transformNative').and.callThrough();
 myPrivateSpy.call(service, {name: 'PR'});

注意:这里的 service 是 Class,transformNative 是私有方法,{name: 'PR'} 传递一个对象参数

Note: Here service is the Class, transformNative is the private method and {name: 'PR'} passing an object argument

这篇关于私有方法 使用 Jasmine 进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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