【发布时间】:2019-01-03 06:31:53
【问题描述】:
Angular6 - 单元测试错误“无法读取未定义的属性‘订阅’”
我正在为具有多个依赖项的角度组件编写单元测试。其中一项依赖服务具有一些属性作为可观察对象。我试图模拟此服务,但在标题中抛出错误,
spec.ts
describe('Component', () => {
let mockService= jasmine.createSpyObj(['property1', 'property2', 'property3']);
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [testComponent],
providers: [
......someOther,
{ provide service, useValue: mockService},
......someOther
]
}).compileComponents();
fixture = TestBed.createComponent(testComponent);
component = fixture.componentInstance;
}));
it('should be created', () => {
expect(component).toBeTruthy();
});
});
ts 文件有
this.Service.property1.subscribe(() => {})
this.Service.property2.subscribe(() => {})
this.Service.property3.subscribe(() => {})
期望测试用例通过但失败并出现标题中的错误
【问题讨论】:
-
您创建的模拟服务是否具有与实际相同的密钥?
-
是的,我不能分享实际的代码,但它与上面类似
标签: angular karma-jasmine