如何使用玩笑用链承诺测试类?

时间:2018-09-17 16:16:03

标签: javascript typescript unit-testing jestjs

我有一个用例,其中我必须测试promise链,因此在下面的代码中,我尝试模拟实际代码中使用jest.fn()调用的所有函数,但遇到任何错误TypeError: Cannot read property 'then' of undefined 在下面的代码中实现了错误,还是编写了异步承诺链的任何更好的方法?

main.ts

export class ModuleExecutor {
    public execute(moduleName: string): (param1, param2) => any {
        const self = this;
        return function (params: any, responseCallback: (param: any, param2: any) => any) {
            let _mod;
            let _httpRequest;
            let _params;
            Promise.resolve(getApiModule(self.identity, moduleName))
                .then((mod: ModuleBase<any, any>) => {
                    _mod = mod;
                    mod.ExecStage = ExecStage.Init;
                    // @ts-ignore - red squiggly for the return mismatch which are resolved in Grunt
                    return mod.init(getHttpModule(self.identity), params);
                })
                .then((httpRequest: HttpRequestBase) => {
                    _httpRequest = httpRequest;
                    if (_mod.Response().Summary.Header) {
                        throw _mod.Response().Summary;
                    }
                    return httpRequest;
                })
                .then((params1: any) => {
                _params = params2;
                _mod.ExecStage = ExecStage.Core;
                return _mod.core(_params, _httpRequest);
            })
                .catch((e) => {
                return e;
                });
        };
    }
}

main.spec.ts

import {ModuleExecuter} . from './main.ts';
    const _executer = new ModuleExecutor(Identity.node);
        const myMockFunc = jest.fn(() => {
            // _executer.execute("Payments/accountBalance/GetAccount001");
            executer = new ModuleExecutor(Identity.node);
            executerSpy = executer.execute("Payments/accountBalance/GetAccount001");
            const _promise1 = new Promise(function(resolve) {
                // moduleExecutor.execute(params, callback function)
                executerSpy(params, function(data) {
                    resolve(data);
                }).catch((e) => {
                    expect(e).toBeTruthy();
                });
            });
            Promise.resolve(_promise1);
        });
        const myMockFuncExecute = jest.fn(() => {

            Promise.resolve(getApiModule(Identity.node, "Payments/accountBalance/GetAccount"));
        });
        const myMockModuleBase = jest.fn((mod: ModuleBase<any, any>) => {
            // _executer.execute("Payments/accountBalance/GetAccount001");
            mod.ExecStage = ExecStage.Init;
            // @ts-ignore - red squiggly for the return mismatch which are resolved in Grunt
            Promise.resolve(mod.init(getHttpModule(Identity.node), params));
        });
        const myMockHttpRequest = jest.fn((httpRequest: HttpRequestBase) => {
            Promise.resolve(httpRequest);
        });

        it('should your test scenario', (done) => {
            myMockFunc()
                .then((data) => {
                    expect(myMockFuncExecute).toBeCalledWith("Payments/accountBalance/GetAccount001");
                    expect(myMockModuleBase).toBeCalledWith(ModuleBase);
                    expect(myMockHttpRequest).toBeCalledWith(HttpRequestBase);
                    expect(data).toEqual({test: "test"});
                    done();
                });
        });

0 个答案:

没有答案