我们想要使用构造函数注入和spyon()注入的场景?哪一个最好? [英] Which scenarios we want to use constructor injection and spyon() injection? and which one is best?

查看:106
本文介绍了我们想要使用构造函数注入和spyon()注入的场景?哪一个最好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Componenet MaintainCOCComponent 构造函数有 MaintainCOCService 的参数,它有API调用方法服务。

  export class MaintainCOCComponent {

构造函数(
private maintaincocservice:MaintainCOCService){}
}



构造函数注入:



这里我配置了testBed,并通过提供商注入模拟 MockMaintainCOCService 服务

  TestBed.configureTestingModule({
imports:[FormsModule,RouterTestingModule.withRoutes([])],
声明:[MaintainCOCComponent],
提供者:[
{提供:MaintainCOCService ,useClass:MockMaintainCOCService}]
});



SpyOn注射



这里我嘲笑通过使用SpyOn实现真正的服务。

 提供者:[
{提供:MaintainCOCService,useClass:MaintainCOCService}]

_maintainCOCService = fixture.componentRef.injector.get(MaintainCOCService);
spyOn(_maintainCOCService,'Method1')
.and.callFake(function(key,value){
return 1;
});
spyOn(_maintainCOCService,'Method2')
.and.callFake(function(key,value){
return 2;
});




我们可以直接在提供程序中传递模拟服务,而不是模拟每个方法使用spyon。我们想要使用哪种场景使用构造函数注入以及我们想要使用Spyon Injection的场景?哪一个最好?



解决方案

通常,什么时候需要嘲笑整个服务,您将使用构造函数注入模拟,例如,您有一个在其内部使用FB SDK的服务,您不希望您的测试将退出 对FB来说,通常最好的做法就是构建一个实现相同界面并在整个应用程序中模拟它的模拟服务。



当你需要检查一些获得正确params的服务时,spyOn 方法通常是use-full。在函数调用之后,该服务的内容通常不那么有趣。



Small Note,有时使用 spyOn 可以表示您正在测试实现而不是规范。 / p>

My Componenet MaintainCOCComponent constructor have an parameter for MaintainCOCService which is have API call method service.

export class MaintainCOCComponent {

constructor(
   private maintaincocservice: MaintainCOCService) { }
}

Constructor injection:

Here i have configured testBed and injected with a mock MockMaintainCOCService service via provider

TestBed.configureTestingModule({
  imports: [FormsModule, RouterTestingModule.withRoutes([])],
  declarations: [MaintainCOCComponent],
  providers: [
    { provide: MaintainCOCService, useClass: MockMaintainCOCService }]
});

SpyOn Injection

Here I have mocked that real service by using SpyOn.

providers: [
            { provide: MaintainCOCService, useClass:MaintainCOCService }]

_maintainCOCService = fixture.componentRef.injector.get(MaintainCOCService);
spyOn(_maintainCOCService , 'Method1')
            .and.callFake(function (key, value) {
                return 1;
            });
 spyOn(_maintainCOCService , 'Method2')
            .and.callFake(function (key, value) {
                return 2;
            });

we can directly pass the mock service in provider instead of mocking every method using spyon.So which scenario we want use Constructor injection and which scenario we want to use Spyon Injection? and and which one is best?

解决方案

Usually, when ever you need to mock the entire service, you will use the Constructor Injection mock, for example, you have a service that uses FB SDK on its internals, you don't want that your tests will go "out" to FB, so usually the best practice it to build a mock Service that implements the same interface and mock it in the entire app.

The spyOn method usually is use-full when you need to check some service that got the correct params. what that service doe's after the function call is usually not so interesting.

Small Note, sometimes the usage of spyOn can indicate that you are testing the implementation rather then the specification.

这篇关于我们想要使用构造函数注入和spyon()注入的场景?哪一个最好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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