如何用茉莉花角编写在管道函数内编写的代码的测试用例?

时间:2019-06-03 08:28:42

标签: angular testing jasmine

我已经在我的服务文件中用角度编写了一个函数。但是我面临的问题是,在编写测试用例时,我还不知道如何测试我们使用带效果的管道的功能

<form name="loginForm" method="post" action="login">

但是函数中的行

fetch(createdBy: string) {
        const millisInAWeek = 7 * 24 * 60 * 60 * 1000;
        const previousWeek = Date.now() - millisInAWeek;
        const query = ref => ref
            .where('createdBy', '==', createdBy)
            .where('updatedAt', '>', previousWeek)
            .orderBy('updatedAt', 'desc')
            .limit(25);
        return this.afsDb.collection('searches', query)
            .get()
            .pipe(
                map(snapshot => snapshot.docs.map(documentSnapshot => documentSnapshot.data() as SearchRecord)),
                switchMap(searchRecords => of(new ActionLoadSearchRecords({ searchRecords })))
            );
    }

Testcase Written by me

it('should fetch top 25 searchRecord in descending Order', () => {
        const createdBy = 'User1';
        const pipeSpy = jasmine.createSpy('pipeSpy');
        const getSpy = jasmine.createSpy('getSpy');

        const collectionSpy = spyOn((service as any).afsDb, 'collection').and.callFake(() => ({
            get: getSpy.and.callFake(() => ({
                pipe: pipeSpy.and.returnValue(of(new ActionLoadSearchRecords({ searchRecords: [searchRecord] })))
            }))
        }));
        service.fetchSearches(createdBy).subscribe(() => {
            expect(pipeSpy).toHaveBeenCalled();
            expect(getSpy).toHaveBeenCalled();
            expect(collectionSpy).toHaveBeenCalled();
        });
    });

0 个答案:

没有答案