angular - 在 .pipe angular 8 jasmine 内部测试可观察到
问题描述
我在我的组件中订阅了主题,triggerRuleExecutionService
该主题正在next()
从另一个组件中发出。
在管道内部,我switchMap
必须调用 http 服务才能从数据库中获取数据
this.ruleExecutionService = this.editCheckSVC.triggerRuleExecutionService.pipe(
switchMap(res => {
return this.editCheckSVC.executeRules(res);
})
).subscribe(res => {
console.log(res);
});
上面的代码在里面ngOnInit
以下是我测试上述功能的规范。
const ruleExecutionSubject = new Subject();
class EditChkManagementServiceStub {
triggerRuleExecutionService = ruleExecutionSubject.asObservable();
executeRules() {
return of([])
}
}
describe('EditcheckManagmentComponent', () => {
let component: EditcheckManagmentComponent;
let fixture: ComponentFixture<EditcheckManagmentComponent>;
let debugElement: DebugElement;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [EditcheckManagmentComponent],
schemas: [NO_ERRORS_SCHEMA],
providers: [{ provide: EditCheckManagementService, useClass: EditChkManagementServiceStub }, HttpService],
imports: [HttpClientModule]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(EditcheckManagmentComponent);
component = fixture.componentInstance;
debugElement = fixture.debugElement;
fixture.detectChanges();
});
it('should call rule execution API', () => {
ruleExecutionSubject.next({
formName: '',
schedule: '',
subSchedule: '',
version: '',
fiscalYear: '2018',
accountingPeriod: '6'
});
fixture.detectChanges();
fixture.whenStable().then(() => {
const executionServiceInstance: EditCheckManagementService = TestBed.get(EditCheckManagementService);
spyOn(executionServiceInstance, 'executeRules').and.callThrough();
component.ngOnInit()
expect(executionServiceInstance.executeRules).toHaveBeenCalled();
});
});
});
测试用例失败并显示消息Expected spy executeRules to have been called.
我在这里做错了什么?
解决方案
describe('EditcheckManagmentComponent', () => {
let component: EditcheckManagmentComponent;
let fixture: ComponentFixture<EditcheckManagmentComponent>;
let debugElement: DebugElement;
let mockEditChkManagementService: any;
beforeEach(async(() => {
mockEditChkManagementService = jasmine.createSpyObj('editCheckSVC', ['executeRules']);
// can maybe create a variable BehaviorSubject so you can call next on it and send new values.
mockEditChkManagemenetService.triggerRuleExecutionService = new BehaviorSubject({
formName: '',
schedule: '',
subSchedule: '',
version: '',
fiscalYear: '2018',
accountingPeriod: '6'
});
TestBed.configureTestingModule({
declarations: [EditcheckManagmentComponent],
schemas: [NO_ERRORS_SCHEMA],
providers: [{ provide: EditCheckManagementService, useValue: mockEditChkManagemenetService}],
imports: [HttpClientTestingModule] // bring in the testing HTTP, not actual implementation
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(EditcheckManagmentComponent);
component = fixture.componentInstance;
debugElement = fixture.debugElement;
// make mockEditChkManagementService return an empty array
mockEditChkManagementService.executeRules.and.returnValue(of([]));
// this first fixture.detectChanges() will call ngOnInit, no need to do it manually
fixture.detectChanges();
});
it('should call rule execution API', () => {
expect(mockEditChkManagementService.executeRules).toHaveBeenCalledWith({
formName: '',
schedule: '',
subSchedule: '',
version: '',
fiscalYear: '2018',
accountingPeriod: '6'
});
});
});
试试看。要进行更多测试并进行简单设置,可能会很困难。您将不得不利用更多describe
的 s、beforeEach
等。
至于你有什么,我不确定有什么问题。
推荐阅读
- node.js - 为什么我在 gitbash 中的节点代码的输出有几个行空格
- reactjs - 升级/降级音频 - 视频
- django - 有没有办法通过更改名称来避免 django 再次上传同名文件
- sql - SQL Server 2012 查找存储桶明智的产品和日期
- c# - 在c#asp.net中删除动态创建的gridviews中的行
- c# - PayPal Checkout.js 弹出 OnCancel 事件在 Edge 浏览器中自动触发
- javascript - How to make query string values case insensitive in angularjs?
- java - 我有一个代码可以从 selenium java 中的 csv 文件中读取数据,如果任何单元格为空,我想执行此操作,然后跳过此单元格并自动读取下一个
- c - 用于 Linux 和 Windows 的 wprintf 可移植代码
- python - How to query an index in Elastic Search for anything that matches