如何使用jasemine检查测试用例响应?

时间:2018-03-23 20:13:52

标签: javascript typescript testing jasmine

我正在为api编写测试用例我已经传递了params并且主文件方法正在调用http请求来获取数据,所以core()基本上是调用api并获得响应。这只是我们必须使用jasemine进行测试的后端代码。

1-首先,如果这与测试中定义的成功相匹配,如何在测试用例中看到响应。

2-在下面的场景中编写测试的正确方法是什么。

balance.spec.ts

import {GetAccountBalance} from "./GetAccountBalance.node";
import {Promise} from "es6-promise";
import {HttpRequest} from "../../../core/http/HttpRequest.node";
import * as sinon from "sinon";
import {getValidationError} from "../../../common/ValidationErrorFactory";

// for easy mocking and cleanup
const sandbox = sinon.createSandbox();
afterAll(function afterTests() {
    sandbox.restore();
});

describe("getAccountBalance", () => {
    // the module under test
    const module = new GetAccountBalance();
    const testURL = 'https://essdd.max.com:4535';
    const urlPath = '/webServices/instrument/accountBalance';
    const fullURL = testURL + urlPath;
    const options = {isJSON: true};
    let result;
    const stubbedEC = sandbox.spy(getValidationError);
    const stubbedHttp = sandbox.createStubInstance(HttpRequest);
    const success = {
        "header": {
            "serviceName": "accountBalance",
            "statusCode": "0000",
            "statusDesc": "SUCCESS"
        },
        "response": {
            "balanceAccount": "6346.44"
        }
    };
  const params = {Id: "21544", appName: "instrucn",  channelName: "Web"};

    describe("core() is called with correct params", function() {
        beforeEach(() => {
            result = module.core(params, stubbedHttp);
        });
        it("it should return the response from server", function() {
            // Invoke the unit being tested as necessary
            result.then((data) => {
                expect(data).toBe(success);
            });

        });

    });


});

getaccountBalnce.ts

  public core(args: IAccountBalanceParam, httpRequest: HttpRequestBase): Promise<any> {
        console.log('enter core');

        const DBPLurl: string = this.constructDBPLurl(args);

        const DBPLrequest: IAccountBalanceDBPLParam = this.constructDBPLrequest(args);

        return Promise.resolve(httpRequest.makeRequest(DBPLurl, DBPLrequest, {isJSON: true}));
    }

0 个答案:

没有答案