我有一个私人的attributeNameSubject. Subject有一个setAttributeName方法将字符串值传递给主题。我们使用getAttributeName.获得对该主题的参考我试着测试上面的代码,但我总是得到false-positive,,即测试通过,但我得到的测试没有预期的警告。结果,它根本没有调用订阅方法。
我正在测试这段代码的角度7。
private readonly attributeNameSubject = new Subject<string>();
get getAttributeName(): Subject<string> {
return this.attributeNameSubject;
}
setAttributeName(value: any) {
this.getAttributeName.next(value.attributeName);
}
it('should set attribute name on valid input', () => {
service = TestBed.get(AttributeService);
service.setAttributeName('some random string');
service.getAttributeName.subscribe((data: string) => {
expect(data).toEqual('some random string');
});
});发布于 2019-11-01 20:02:59
您的代码有两个问题。
setAttributeName向订阅者发出值,而getAttributeName则听可观察的值。因此,当您调用setAttributeName时,getAttributeName会发出一个值,但是没有任何订阅。因此,您应该首先订阅getAttributeName,然后调用setAttributeName来发出值。,
getAttributeName在传递字符串时发出value.attributeName。您需要传递一个对象。这是工作测试用例。
it('should set attribute name on valid input', () => {
service = TestBed.get(AttributeService);
service.getAttributeName.subscribe((data: string) => {
expect(data).toEqual('some random string');
});
service.setAttributeName({ attributeName: 'some random string' });
});https://stackoverflow.com/questions/58665016
复制相似问题