假设我有一个订阅服务功能的组件:
export class Component {
...
ngOnInit() {
this.service.doStuff().subscribe(
(data: IData) => {
doThings(data);
},
(error: Error) => console.error(error)
);
};
};
Run Code Online (Sandbox Code Playgroud)
订阅调用将两个匿名函数作为参数,我已设法为数据函数设置工作单元测试,但Karma不接受错误一的覆盖.
我已经尝试过监视console.error函数,抛出一个错误,然后期待调用间谍已被调用,但这并没有完全做到.
我的单元测试:
spyOn(console,'error').and.callThrough();
serviceStub = {
doStuff: jasmine.createSpy('doStuff').and.returnValue(Observable.of(data)),
};
serviceStub.doStuff.and.returnValue(Observable.throw(
'error!'
));
serviceStub.doStuff().subscribe(
(res) => {
*working test, can access res*
},
(error) => {
console.error(error);
console.log(error); //Prints 'error!' so throw works.
expect(console.error).toHaveBeenCalledWith('error!'); //Is true but won't be accepted for coverage.
}
);
Run Code Online (Sandbox Code Playgroud)
测试这些匿名函数的最佳实践是什么?确保测试覆盖率的最低要求是什么?
typescript karma-coverage angular2-testing angular2-observables angular
我创建了一个ApiService类来处理我们的自定义API查询,同时使用我们自己的序列化程序+其他功能.
ApiService的构造函数签名是:
constructor(metaManager: MetaManager, connector: ApiConnectorService, eventDispatcher: EventDispatcher);
Run Code Online (Sandbox Code Playgroud)
MetaManager 是一种处理api的metadatas的可注射服务.ApiConnectorService是一个包装的服务,Http用于添加我们的自定义标题和签名系统.EventDispatcher 基本上是Symfony的事件调度系统,在打字稿中.当我测试时ApiService,我做了一个初始化beforeEach:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports : [
HttpModule
],
providers: [
ApiConnectorService,
ApiService,
MetaManager,
EventDispatcher,
OFF_LOGGER_PROVIDERS
]
});
}));
Run Code Online (Sandbox Code Playgroud)
它工作正常.
然后,添加我的第二个规范文件,该文件是ApiConnectorService,与此beforeEach:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports : [HttpModule],
providers: [
ApiConnectorService,
OFF_LOGGER_PROVIDERS,
AuthenticationManager,
EventDispatcher
]
});
}));
Run Code Online (Sandbox Code Playgroud)
并且所有测试都因此错误而失败:
错误:无法解析ApiService的所有参数:(MetaManager,?,EventDispatcher).
api-connector-service.spec.ts(ApiConnectorService的spec文件),ApiService测试将成功.api-service.spec.ts(ApiService …karma-jasmine angular2-services angular2-injection angular2-testing angular
我正在使用Angular 2 final(2.0.1).我有一个提供服务的组件.它是唯一使用它的人,这就是为什么它提供它而不是包含模块,它也被注入到构造函数中.
@Component({
selector: 'my-comp',
templateUrl: 'my-comp.component.html',
styleUrls: ['my-comp.component.scss'],
providers: [MyService],
})
export class MyComponent {
constructor(private myService: MyService) {
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试实现规范时,它失败了.
describe("My Component", () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [MyComponent],
providers: [
{
provide: MyService,
useClass: MockMyService
},
]
});
this.fixture = TestBed.createComponent(MyComponent);
this.myService = this.fixture.debugElement.injector.get(MyService);
});
describe("this should pass", () => {
beforeEach(() => {
this.myService.data = [];
this.fixture.detectChanges();
});
it("should display", () => {
expect(this.fixture.nativeElement.innerText).toContain("Health");
});
});
Run Code Online (Sandbox Code Playgroud)
但是,当我将服务提供声明从组件移动到包含模块时,测试通过.
我假设这是因为TestBed测试模块定义了模拟服务,但是当创建组件时 - 它会使用实际实现覆盖模拟...
有没有人知道如何测试提供服务和使用模拟服务的组件?