简体   繁体   English

如何在角度 6 单元测试中依赖注入接口

[英]How to dependency inject interface in angular 6 unit test

One of the service dependency injects an interface in the constructor.其中一个服务依赖项在构造函数中注入了一个接口。 I wonder, how I can dependency injects the interface in the unit test?我想知道,如何在单元测试中依赖注入接口?

Exported interface:导出接口:

export interface MobilePlatform {
  onClick(): void;
  onPageFinished(router: Router): void;
  onPageStart(): void;
  sendClose(): void;
  tts(text: String): void;
}

Service injects the interface in the constructor服务在构造函数中注入接口

constructor(private platform: MobilePlatform, private router: Router) {}

How I can inject this interface in the angular unit test?我如何在角度单元测试中注入这个接口?

describe('MobileActions', () => {
  let actions: MobileActions;
  let platform: MobilePlatform;

  beforeEach(() => {

    TestBed.configureTestingModule({
      providers: [
        MobileActions,
        { provide: MobilePlatform, useClass: MockMobilePlatform },
        { provide: Router, useClass: MockRouter }
      ]
    });

    actions = TestBed.get(MobileActions);
    platform = TestBed.get(MockMobilePlatform);
  });

  it('should create actions', () => {
    expect(actions).toBeTruthy();
    expect(platform).toBeTruthy();
  });
});

Seems this kind of inject failed.似乎这种注入失败了。

You can't, since an interface is a contract that does not get transpiled to an actual class function.你不能,因为接口是一个不会被转换为实际类函数的契约。 In order to create a testable representation of such interface in the Angular injector, you will want to create a typed injection token:为了在 Angular 注入器中创建此类接口的可测试表示,您需要创建一个类型化的注入令牌:

Somewhere in your MobilePlatform models file:在您的 MobilePlatform 模型文件中的某处:

export const MOBILE_PLATFORM = new InjectionToken<MobilePlatform>('mobilePlatform');

Then in your service constructor:然后在您的服务构造函数中:

constructor( @Inject(MOBILE_PLATFORM) private platform: MobilePlatform, private router: Router ) {}

Finally, in the providers array of your testing module:最后,在测试模块的providers数组中:

{ provide: MOBILE_PLATFORM, useClass: MockMobilePlatform },

I couldn't achieve this using TestBed, instead I use mock classes like this我无法使用 TestBed 实现此目的,而是使用像这样的模拟类

class MobilePlatformMockClass implements MobilePlatform {
    // implement interface mock functions
}

describe('MobileActions', () => {
  let actions: MobileActions;
  let platform: MobilePlatform;

  beforeEach(() => {
    const mobilePlatformMock = new MobilePlatformMockClass();
    const routerMock = { navigate: () => {} };
    actions = new MobileActions(mobilePlatformMock, routerMock)
  });

  it('should create actions', () => {
    expect(actions).toBeTruthy();
  });
});

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

相关问题 如何对具有依赖性的函数进行单元测试(角度) - How to unit test a function with a dependency (angular) 在 Angular 和 JEST 的单元测试中没有 @Inject(&#39;environment&#39;) 的提供者 - No provider for @Inject('environment') in Unit Test with Angular & JEST Angular2 在单元测试中注入 ElementRef - Angular2 Inject ElementRef in unit test 如何将依赖项注入到 Angular 8 中的 class - How to inject dependency in to a class in Angular 8 如何使用 Jasmine 处理对 Angular 单元测试的依赖注入? - How to handle dependency injection into Angular unit test using Jasmine? Angular 7:如何在 Jasmine 单元测试中解决 Hammer.js 依赖关系 - Angular 7: How to resolve Hammer.js dependency in Jasmine unit test Angular 2单元测试:如何重写@Inject(&#39;Window&#39;)private _window:Window构造函数依赖项? - Angular 2 unit tests: How to override @Inject('Window') private _window: Window constructor dependency? 使用Angular 2注入依赖项的单元测试服务 - Unit test service with injected dependency using Angular 2 Angular2单元测试+创建接口的对象 - Angular2 unit test + creating object of an interface 如何将IterableDiffers“服务”注入到单元测试中 - how to inject IterableDiffers “service” into a unit test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM