[英]angular unit test function.pipe is not a function
I am aware there is a similar question here 我知道有一个类似的问题在这里
but I am doing what the suggested answer says and I am still getting this error 但我正在执行建议的答案,但仍然出现此错误
basically I have a function like so 基本上我有这样的功能
checkForInvitation() {
this._join.joinInformationSource.pipe(filter(x => x !== null)).subscribe(result => {
this.joinInformation = result;
this.setFields(result.userInfo);
});
}
basically it gets information and then calls another method to prepopulate some form fields. 基本上,它获取信息,然后调用另一种方法来预填充某些表单字段。
now I am trying to test this method so I have created a spy like so... 现在我正在尝试测试此方法,所以我创建了一个像这样的间谍...
// ...
const joinServiceSpy = jasmine.createSpyObj('JoinService', ['joinInformationSource']);
// ...
providers: [
{ provide: JoinService, useValue: joinServiceSpy }
]
// ...
joinService = TestBed.get(JoinService);
it('should prepopulate fields if there is join information', () => {
let joinInfoSpy = joinService.joinInformationSource.and.returnValue(
of(
// ... object
)
)
});
now when I run ng test I get this error repeatedly 现在当我运行ng test时,我反复得到此错误
this._join.joinInformationSource.pipe is not a function
this is my joinService 这是我的joinService
joinInformationSource = new BehaviorSubject<JoinInformation>(null);
setJoinInformation(joinInformation: JoinInformation) {
this.joinInformationSource.next(joinInformation);
}
What am I doing wrong here?? 我在这里做错了什么?
According to the documentation createSpyObj accepts a list of methods as a second parameter. 根据文档, createSpyObj接受方法列表作为第二个参数。 So when you're creating the mocking object, you create joinInformationSource
as a function. 因此,当您创建joinInformationSource
对象时,可以将joinInformationSource
创建为一个函数。
const joinServiceSpy = jasmine.createSpyObj('JoinService', ['joinInformationSource']);
//now joinSeverSpy contains method joinInformationSource
But in your code you use joinInformationSource
as a field 但是在您的代码中,您使用joinInformationSource
作为字段
// _join.joinInformationSource has been used as a field
this._join.joinInformationSource.pipe(filter(x => x !== null)).subscribe(result => {
this.joinInformation = result;
this.setFields(result.userInfo);
});
And since joinInformationSource
is a function it definitely doesn't have a pipe
method. 而且由于joinInformationSource
是一个函数,所以它肯定没有pipe
方法。 There are few solutions. 解决方案很少。 One of them is using of spyOnProperty method: 其中之一是使用spyOnProperty方法:
//create a service object and define a property
const joinService: any = new Object();
Object.defineProperty(joinService, 'joinInformationSource', {get: () => {}});
//then create a spy on the property
it('should prepopulate fields if there is join information', () => {
let joinInfoSpy = spyOnProperty(joinService, 'joinInformationSource', 'get')
.and.returnValue(
new BehaviorSubject<JoinInformation>(//object)
)
}
//the rest of the code
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.