茉莉花单元测试可观察的订阅不会触发 [英] Jasmine unit testing observable subscribe does not trigger
问题描述
我正在将Angular 5与Jasmine和Karma一起使用.我正在尝试测试某个功能是否有效,但是我的订阅在单元测试期间不会触发.当我使用茉莉花的完成功能时,这会导致我的单元测试失败. 我想使此单元测试成功.
I'm using Angular 5 with Jasmine and Karma. I'm trying to test whether a certain function works but my subscribe doesn't trigger during unit testing. this causes my unit test to fail as I'm using the done function of jasmine. I want to make this unit test succeed.
我将超时间隔设置为20秒,以查看是否花了一段时间(不应该这样).
I've set the timeout interval to 20 seconds to see if it just took a while (which it shouldn't).
我也尝试过使用async和fakeasync,但是它不会触发.我可以进行订阅触发吗?
I've tried to use async and fakeasync as well, but it doesn't trigger. Is it possible for me to make the subscription to trigger?
这是我得到的代码:
describe('FilterService', () => {
let service: FilterService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [FilterService]
});
service = TestBed.get(FilterService);
jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000;
});
it("SetItemsToFilterByAndApplyFilters should set ItemsToFilterBy and set the InFilter property", (done: DoneFn) => {
//arrange
let item1: any = new Organisation();
item1.Id = 1;
item1.InFilter = true;
let item2: any = new Organisation();
item1.Id = 2;
item1.InFilter = false;
let itemNew1: any = new Organisation();
itemNew1.Id = 1;
let itemNew2: any = new Organisation();
itemNew2.Id = 2;
service.SetItemsToFilterBy([item1, item2]);
let spy = spyOn(service.ItemsToFilterBy$, 'subscribe');
//act
service.SetItemsToFilterByAndApplyFilters([itemNew1, itemNew2]);
//assert
service.ItemsToFilterBy$.subscribe(result => {
let result1 = _.find(result, item => {
return item.Id == itemNew1.Id;
});
let result2 = _.find(result, item => {
return item.Id == itemNew2.Id;
});
expect(result1.InFilter).toBeTruthy();
expect(result2.InFilter).toBeFalsy();
done();
});
});
});
这是与Basavaraj Bhusani一起使用的代码
this is the code that worked with thanks to Basavaraj Bhusani
it("SetItemsToFilterByAndApplyFilters should set ItemsToFilterBy and set the InFilter property", (done: DoneFn) => {
//arrange
let spy = spyOn(service.ItemsToFilterBy$, 'subscribe').and.callThrough();
let item1: any = new Organisation();
item1.Id = 1;
item1.InFilter = true;
let item2: any = new Organisation();
item2.Id = 2;
item2.InFilter = false;
let itemNew1: any = new Organisation();
itemNew1.Id = 1;
let itemNew2: any = new Organisation();
itemNew2.Id = 2;
service.SetItemsToFilterBy([item1, item2]);
//act
service.SetItemsToFilterByAndApplyFilters([itemNew1, itemNew2]);
//assert
service.ItemsToFilterBy$.subscribe(result => {
let result1 = _.find(result, item => {
return item.Id == itemNew1.Id;
});
let result2 = _.find(result, item => {
return item.Id == itemNew2.Id;
});
expect(result1.InFilter).toBeTruthy();
expect(result2.InFilter).toBeFalsy();
done();
});
expect(spy).toHaveBeenCalled();
});
});
推荐答案
如果service.ItemsToFilterBy$
是Subject<any>()
,而不是BehaviorSubject
或ReplaySubject
,请在规范开始时订阅service.ItemsToFilterBy$
.在此处阅读.
If service.ItemsToFilterBy$
is Subject<any>()
, not BehaviorSubject
or ReplaySubject
, subscribe to service.ItemsToFilterBy$
in the beginning of your spec. Read here.
监视方法时,请按jasmine
使用callThrough()
方法.
spyOn(service.ItemsToFilterBy$, 'subscribe')
Use the callThrough()
method by jasmine
when you spy on a method.
spyOn(service.ItemsToFilterBy$, 'subscribe')
即
const spy = spyOn(service.ItemsToFilterBy$, 'subscribe').and.callThrough();
在此处 https://jasmine.github.io/2.0/introduction了解有关callThrough
的信息. .html
Read about callThrough
here https://jasmine.github.io/2.0/introduction.html
这篇关于茉莉花单元测试可观察的订阅不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!