如何用茉莉花大理石测试受试者

时间:2018-08-29 15:37:38

标签: angular rxjs jestjs jasmine-marbles

Angular 6,Rxjs,Jest,茉莉花大理石。

非常常见的情况:搜索服务器端项目的组件。 在该组件中,有一些控件可以更改搜索条件,我想以“反应式”的方式进行编码。所以在组件代码中,我有这样的东西:

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);
  }
}

也就是说,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();
});

现在我想使用茉莉花大理石转换此测试,但是我不知道如何...

我想要这样的东西:

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)
    });
};

1 个答案:

答案 0 :(得分:0)

测试的主要目标是模拟依赖关系,并且不更改测试单元SearchComponent内部的任何内容。

因此,stubSubject(component.searchConditions$, stream)component.searchConditions$ = cold是不好的做法。

因为我们要在searchConditions$中调度发射,所以我们需要有一个内部调度流,我们也可以在这里使用coldhot

源数据(对不起,我猜有些类型)


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)。