我正在开发一个 Angular 4 应用程序,编写单元案例,我面临以下问题。
组件.ts
ngOnInit() {
this.service.getData().subscribe(res => {
console.log(res);
this.data= JSON.parse(res._body);
this.addFormControls();
},
error => {
console.log(error);
});
}
组件.spec.ts
describe('component', () => {
let userRolesService: LayoutService;
const modulesData = {
'moduleName':'Info'
}
class MockUserRolesService {
public getModulesData() {
return modulesData;
}
}
beforeEach(async(() => {
TestBed.configureTestingModule({
providers:[ {provide: LayoutService, useClass: MockUserRolesService } ]
})
.compileComponents();
}));
beforeEach(() => {
userRolesService = TestBed.get(LayoutService);
});
afterEach(() => {
userRolesService = null;
component = null;
});
it('should fetch module data', () => {
userRolesService.getData().subscribe(
result => {
expect(result).toBeTruthy();
}
);
});
});
更新了模拟服务文件的代码。你能帮我一个人让它工作吗?使用模拟服务和测试床框架。你能请人帮帮我吗…………………………………………………………………………………… ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ...................................................