小编yuf*_*ieh的帖子

Angular 6 测试 Jasmine Karma:覆盖提供程序不起作用

所以我在网上看了很多问题,包括这个 https://github.com/angular/quickstart/issues/320 ,我很难过......

我如何设置我的代码是我的主要描述创建了我的测试平台组件,在这里我为活动路由设置了我的 mockParams,所以我们可以 this.route.queryparams.subscribe(..),我的问题是我无法覆盖不同的 describe 或 'it' 块中的值。

 beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [MyComponent],
      imports: [RouterTestingModule
        })],
      providers: [
        { provide: ActivatedRoute, useValue: mockParams },
      ],
      schemas: [NO_ERRORS_SCHEMA]
    }).compileComponents();
}));
Run Code Online (Sandbox Code Playgroud)

这是我在不同的 NESTED 描述块中添加覆盖的示例...

beforeEach(() => {
      TestBed.overrideProvider(ActivatedRoute, 
      {useValue: newMockParams});
      TestBed.compileComponents();
      fixture = TestBed.createComponent(MyComponent);
      component = fixture.componentInstance;
      fixture.detectChanges();
});
Run Code Online (Sandbox Code Playgroud)

几乎就像这根本没有运行...如果我重新使用 mockParams 并更改值,新的模拟参数不会更改,然后它将更改原始描述中的值,我真的必须重新创建我的组件吗?每个嵌套描述?当我唯一需要更改的是提供程序时,我必须这样做是不正确的,我不确定此时 overrideProvider 甚至做了什么!任何帮助将不胜感激!

javascript unit-testing jasmine karma-jasmine angular

6
推荐指数
1
解决办法
4727
查看次数