【问题标题】:How do we mock private method using jasmine?我们如何使用茉莉花模拟私有方法?
【发布时间】:2020-02-27 00:55:39
【问题描述】:

试图模拟类中的私有方法无法获得任何成功,我正在使用 jasmine,所以在下面的代码中我有 getDrug 正在进行 http 调用的方法,现在我可以模拟核心但我如何存根/模拟 getDrug 以提高代码覆盖率。

DrugPriceApi.node.ts

export class DrugPriceApi extends Types.ModuleBase < DrugPriceParam, DrugPriceResultSet[] > {

        private _memberId: string = "";
        private _dependencies: any;
        private errorMessage: string = "There was an issue while retrieving drug price. Please try again.";

        before(args: DrugPriceParam): Promise < DrugPriceParam > {
            args.daysSupply = args.daysSupply ? args.daysSupply : args.appName === "VOYA" ? "90" : "BLNK";
            return Promise.resolve(args);
        }

        core(args: DrugPriceParam, requestMethod: Interface.Core.RequestMethod, _dependencies: any): Promise < any > {
                this._dependencies = _dependencies;

                return new Promise < any > ((resolve: Function, reject: Function) => {
                        this.getDrug(function(resolve) {
                                resolve( //response here);});
                                }
                            }

                        }
                    }
                }

                private getDrug(args: DrugPriceParam, requestMethod: Interface.Core.RequestMethod) {
                    return requestMethod.http.makeRequest({
                        url: {
                            name: 'domain_getDrug',
                            params: {
                                version: '1.0',
                                appName: args.appName ? args.appName : 'WEB',
                                tokenId: args.tokenId,
                                refId: args.refId,
                                internalID: args.memberInfo.internalID,
                                searchText: args.drugName,
                                drugNdcId: 'BLNK'
                            }
                        },
                        body: {
                            memberInfo: args.memberInfo
                        }
                    });
                }

DrugPriceApi.spec.ts

       import {
            DrugPriceApi
        } from "./DrugPriceApi.node";

        it("should get drugByName", function(done) {
            let getDrug: any;
            beforeEach((done) => {
                function getMockData(url: string): Promise < any > {
                    const rtnval = {
                        "details": [],
                        "header": {
                            "statusDesc": "Success",
                            "statusCode": "0000",
                            "IndexOfRequestThatFailed": []
                        }
                    };

                    return Promise.resolve(rtnval);
                }

                let moderator: Interface.Mock.Handler = {
                    getMockData: getMockData
                };
                getDrug = moderator;

            });
            let param: any;
            param.daysSupply = "BLNK";
            param.quantity = "BLNK";
            param.memberId = "1372";
            const result: any = _sdkInstance.SDK.Pricing.getDrugPrice(param);
            result.then(function(res: any) {
                spyOn(DrugPriceApi.prototype, "core").and.callFake(() => {
                        expect(DrugPriceApi.prototype.getDrug).toHaveBeenCalled();
                });
                expect(res.Header.StatusCode).toBe("5000");
                done();
            });
        });

【问题讨论】:

    标签: javascript typescript unit-testing jasmine


    【解决方案1】:

    我对这里发生的事情以及为什么对 DrugPriceApi 进行间接测试感到有些困惑。看起来我们正在测试 Pricing.getDrugPrice 而不是 DrugPriceApi。

    如果您想测试 DrugPriceApi.core,那么您只需创建它的一个实例并设置 DrugPriceApi.getDrug = jasmine.createSpy();

    const api = new DrugPriceApi();
    api.getDrug = jasmine.createSpy();
    api.core(...whatever).then(() => {
        expect(api.getDrug).toHaveBeenCalled();
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 2016-06-08
      • 1970-01-01
      相关资源
      最近更新 更多