测试承诺时Jasmine忽略测试

时间:2017-05-02 02:32:44

标签: reactjs jasmine karma-runner

我遇到了一个奇怪的问题。根据我的日志消息,我正在测试的函数正常工作,但它似乎忽略了我试图提供它的任何getSessionContext(): Promise<ISessionContext> { let query = '{\ user {\ userId\ clientId\ partnerId\ userType\ visibleId\ firstName\ lastName\ emailAddress\ profileImageUrl\ }\ partner {\ id\ clients {\ clientId\ partnerId\ clientName\ }\ }\ }'; let success = action("AuthService.getSessionContext", (sessionContext: ISessionContext) => { if (!sessionContext.user) { throw new Error('Request failed'); } this.sessionContext = sessionContext; this.userInfo = sessionContext.user; this.activeClientId = this.userInfo.clientId; //default to primary client this.isLoggedIn = true; return sessionContext; }); return this.graphQLService.query({query}, {unscoped: true}) .then(success, (data) => { console.error('Unable to get session information', data); }) as Promise<ISessionContext>; } 断言。

这是测试中的方法:

fdescribe('::getSessionContext', () => {
    it('returns a valid sessionContext', () => {
        spyOn(svc.graphQLService, 'query').and.returnValue(promise_STUB);

        console.log(`sessionContext BEFORE ${JSON.stringify(svc.sessionContext)}`);
        expect(svc.sessionContext.user).toBeFalsy();
        console.log(`userInfo BEFORE ${JSON.stringify(svc.userInfo)}`);

        svc.getSessionContext().then
        (
            (result) => {
                console.log(`****************************`);
                console.log(`final result ${JSON.stringify(result)}`);
                console.log(`sessionContext AFTER ${JSON.stringify(svc.sessionContext)}`);
                expect(svc.sessionContext.user).toEqual({});

                console.log(`userInfo AFTER ${JSON.stringify(svc.userInfo)}`);
                expect(svc.userInfo).toEqual(promiseValue.user);

                //FIXME expect statements are being ignored
                expect(true).toBeFalsy();
                done();
            }
        );
    });
});

这是我目前的规范:

LOG: 'sessionContext BEFORE {"partner":{"clients":[{"clientId":1234},
{"clientId":1235},{"clientId":1236},{"clientId":1237}]}}'
LOG: 'userInfo BEFORE {"partnerId":1234,"clientId":1235}'
LOG: '****************************'
LOG: 'final result {"user":{}}'
LOG: 'sessionContext AFTER {"user":{}}'
LOG: 'userInfo AFTER {}'

这是终端输出:

{{1}}

如您所见,终端输出确认功能正常。然而,它完全忽略了我试图做出的任何期望陈述。关于为什么会发生这种情况的任何想法?

1 个答案:

答案 0 :(得分:0)

您没有将done回调传递给测试功能。

尝试将it('returns a valid sessionContext', () => {更改为it('returns a valid sessionContext', (done) => {