Angular 6,Rxjs,Jest, Jasmine 大理石。
非常常见的情况:搜索服务器端项目的组件。
在该组件中,有一些可以更改搜索条件的控件,我想以“响应式(Reactive)”的方式进行编码。所以在组件代码中,我有这样的东西:
class SearchComponent implements OnInit {
public searchData: SearchData = {};
public searchConditions$ = new Subject<SearchData>();
constructor(private searchService: SearchService) { }
public results$: Observable<ResultData> = this.searchConditions$.pipe(
distinctUntilChanged(this.compareProperties), // omissis but it works
flatMap(searchData => this.searchService.search(searchData)),
shareReplay(1)
);
// search actions
ngOnInit() {
this.searchConditions$.next(this.searchData);
}
public onChangeProp1(prop1: string) {
this.searchData = { ...this.searchData, prop1 };
this.searchConditions$.next(this.searchData);
}
public onChangeProp2(prop2: string) {
this.searchData = { ...this.searchData, prop2 };
this.searchConditions$.next(this.searchData);
}
}
也就是说,每当UI中的某些内容发生更改时,就会触发Subject
触发搜索条件。现在,我想测试搜索服务将仅针对不同的输入被调用。
我可以用这种方式“不用弹珠”来做:
test('when searchConditions$ come with equal events search service will not be called more than once', (done: any) => {
service.search = jest.fn(() => of(TestData.results));
component.results$.subscribe({
complete: () => {
expect(service.Search).toHaveBeenCalledTimes(1);
done();
}
});
component.searchConditions$.next(TestData.searchCriteria);
component.searchConditions$.next(TestData.searchCriteria);
component.searchConditions$.next(TestData.searchCriteria);
component.searchConditions$.complete();
});
现在,我想使用 Jasmine 大理石转换此测试,但我不知道如何...我想要这样的东西:
test('when searchConditions$ come with equal events search service will not be called more than once', (done: any) => {
service.search = jest.fn(() => of(TestData.results));
component.searchConditions$ = cold('--a--a|', { a : TestData.searchCriteria});
const expected = cold('--b---|', { b : TestData.results});
expect(component.results$).toBeObservable(expected);
});
显然,这是行不通的...更新
以某种方式关闭...使用“测试助手”
test('when searchConditions$ comes with equal events search service will not be called more than once - marble version', () => {
service.search = jest.fn(() => of(TestData.results));
const stream = cold('--a--a|', { a : TestData.searchCriteria});
const expected = cold('--b---|', { b : TestData.results});
stubSubject(component.searchConditions$, stream);
expect(component.results$).toBeObservable(expected);
});
// test helpers
const stubSubject = (subject: Subject<any> , marbles: TestObservable) => {
marbles.subscribe({
next: (value: any) => subject.next(value),
complete: () => subject.complete(),
error: (e) => subject.error(e)
});
};
最佳答案
测试的主要目标是模拟依赖关系,并且不更改测试单元SearchComponent内的任何内容。
因此,stubSubject(component.searchConditions$, stream)
或component.searchConditions$ = cold
是不好的做法。
因为我们要调度searchConditions$
中的发射,所以我们需要一个内部调度流,我们也可以在这里使用cold
或hot
。
源数据(对不起,我猜有些类型)
type SearchData = {
prop?: string;
};
type ResultData = Array<string>;
@Injectable()
class SearchService {
public search(term: SearchData): Observable<any> {
return of();
}
}
@Component({
selector: 'search',
template: '',
})
class SearchComponent implements OnInit {
public searchData: SearchData = {};
public searchConditions$ = new Subject<SearchData>();
constructor(private searchService: SearchService) { }
public results$: Observable<ResultData> = this.searchConditions$.pipe(
distinctUntilKeyChanged('prop'),
tap(console.log),
flatMap(searchData => this.searchService.search(searchData)),
shareReplay(1),
);
// search actions
ngOnInit() {
this.searchConditions$.next(this.searchData);
}
public onChangeProp1(prop1: string) {
this.searchData = { ...this.searchData, prop: prop1 }; // I've changed it to prop
this.searchConditions$.next(this.searchData);
}
public onChangeProp2(prop2: string) {
this.searchData = { ...this.searchData, prop: prop2 }; // I've changed it to prop
this.searchConditions$.next(this.searchData);
}
}
及其测试
test('when searchConditions$ come with equal events search service will not be called more than once', () => {
service.search = jest.fn(() => of(TestData.results));
// our internal scheduler how we click our component.
// the first delay `-` is important to allow `expect` to subscribe.
cold('-a--a--b--b--a-|', {
a: 'a',
b: 'b',
}).pipe(
tap(v => component.searchConditions$.next({prop: v})),
// or like user does it
// tap(v => component.onChangeProp1(v)),
finalize(() => component.searchConditions$.complete()),
).subscribe();
// adding our expectations.
expect(component.results$).toBeObservable(cold('-r-----r-----r-|', {
r: TestData.results,
}));
});
现在,我们无需更改测试单元,而只需更改其依赖项(
service.search
)。
关于angular - 如何用 Jasmine 大理石测试受试者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52081332/