如何使用玩笑来测试诺言链?

时间:2018-09-14 15:41:40

标签: javascript angular unit-testing jestjs jest-fetch-mock

我有一个模块执行程序,它将在每个api请求上执行,现在我正在尝试编写案例以对该执行程序进行单元测试。我保证将基于响应执行返回链。我在测试用例中看到执行诺言的问题,这里将提供适当帮助来测试该用例的任何帮助。

main.ts

export class Executor {
    private passedParam: ILogParams = {} as ILogParams;
    constructor(public identity: Identity) {
        this._ajv = new Ajv();
    }
    public execute(moduleName: string): (param1, param2) => any {
        const self = this;
        // getting rid of the tslint issue with Function
        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;
                    return mod.init(getHttpModule(self.identity), params);
                })
                .then((httpRequest: HttpRequestBase) => {
                    _httpRequest = httpRequest;
                    if (_mod.Response().Summary.Header) {
                        throw _mod.Response().Summary;
                    }
                    return httpRequest;
                })
                .then(() => {
                    // empty the error stack
                    _mod.objErrorHandler.RemoveAllError();
                    _mod.ExecStage = ExecStage.Before;
                    return _mod.before(params);
                })
                .then((params1: any) => {
                    const _validators = _mod.getValidators();
                    let _failed: boolean = false;
                    return params1;
                })
                .then((params2: any) => {
                    _params = params2;
                    _mod.ExecStage = ExecStage.Core;
                    return _mod.core(_params, _httpRequest);
                })
                .catch((data: any) => {
                    const error: IHeader = {} as IHeader;
                })
                .then((data: any) => {
                        responseCallback(data, moduleName.substr(moduleName.indexOf('/') + 1));
                });
        };
    }
}

main.spec.ts

import * as sinon from "sinon";

import {ModuleExecutor} from "./main.ts";
import {ExecStage, Identity} from "../../src/ts/common/Enums";
import ValidateRequestSchema from "../../src/ts/validation/requestSchema.js";
describe("ModuleExecuter", () => {
    const sandbox = sinon.createSandbox();
    afterEach(function afterTests() {
        sandbox.restore();
    });
    let executer;
    let executerSpy;
    let results;
    let stubbedExecutor;
    let apiModule;
    let _this;
    const stubbedExecutorReturnFuction = sandbox.spy(function(args) {
        executer = new ModuleExecutor(Identity.node);
        executerSpy = executer.execute();
        _this = this;
        return new Promise(function(resolve) {
            // moduleExecutor.execute(params, callback function)
            executerSpy(args, function(data) {
                resolve(data.Details);
            });
        });
    });

    const stubbedExecutorReturn = sandbox.spy(function(args, innerFunc) {
        return innerFunc({Details: successResponse});
    });


    beforeEach(function() {
        stubbedExecutor = sandbox.stub(ModuleExecutor.prototype, "execute").callsFake(function() {
            return stubbedExecutorReturn;
        });
        apiModule = new GetAccountBalance();
        const execute = sandbox.spy(stubbedExecutorReturnFuction);
        results = execute("Payments/accountBalance/GetAccountBalance", {test:"test"});
    });


    describe("ModuleExecutor", function() {
        it('should call ModuleExecutor.execute', function () {
            sinon.assert.calledOnce(stubbedExecutor);
        });
        it('should return a promise', function() {
            results.then(function(data) {
                expect(data).toBe(successResponse);
            });
        });
        it('should check validate AJV schema', function() {
            let _mod;
            results.then((mod: ModuleBase<any, any>) => {
                _mod = mod;
                mod.ExecStage = ExecStage.Before;
                const requestSchema = "I" + _mod.__proto__.constructor.name + "Param";
                const classSchema = ValidateRequestSchema[requestSchema];
                const valid = _this._ajv.validate(classSchema, {test:"test"});
                console.log("BOOLEAN>>>>>>>", valid);
                expect(valid).toBeTruthy();
            });
        });

    });
});

0 个答案:

没有答案