javascript-如何在for循环的内部代码中使用角度的jaamine编写单元测试用例?

时间:2019-08-06 09:55:11

标签: angular unit-testing jasmine angular-test angular-testing

我想用茉莉花测试角度的一种功能。

这是我的代码,

it("should call showLog",async( () => {
      let logData = {
        "deployLog": {
        "keys": [
          "keys",
          "storeUrl",
          "deployStartTime",
          "storeName",
          "user",
          "deployStatus",
          "labels"
        ],
        "deployToTalTime": "0.3",
        "storeUrl": "",
        "deployStartTime": "01-08-2019 05-36-56",
        "storeName": "bmw",
        "user": "cbbjbjb",
        "deployStatus": "Deployment Fail",
        "labels": {
          "generateStoreDuration": "store Generation Completed.",
          "deployToTalTime": "Deployment Completed. Time taken is",
          "storeUrl": "Store URL",
          "applicationConfigurationDuration": "Application configuration completed. Time taken is",
          "storeName": "Store",
          "deployStartTime": "Deployment Start Time",
          "applicationPreparationDuration": "Application preparation completed. Time taken is",
          "error": "Error",
          "user": "Deployment Started By",
          "deployStatus": "Deployment Status"
        }
      }
    }
      let spy = spyOn(sharedService, "currentPopupState").and.callFake(function(
        data
      ) {
        expect(data).toEqual("deployLog");
      });




    })) 




  showLog(data: DeployHistory) {
        this.sharedService.changeEditorPopupState(EditorPopupState.deployLog);
        let log = data.deployLog;
        for (let key in log) {
          if (key == "storeUrl") {
            if (log[key]) {
              if (log[key].indexOf("https") > 0) {
                let substr = log[key].split("https");
                let anchor = "https" + substr[1];
                log[key] = `${substr[0]}<a target='_blank' href='${anchor}'>${anchor}</a>`;
              }
            }
          }
        }
    }

我需要测试函数showLog(data)的for循环内部逻辑。

如何为循环编写测试用例?以及大小写逻辑。

如何使用茉莉花测试此代码。

预先感谢

1 个答案:

答案 0 :(得分:0)

在适当的单元测试中,您实际上并不关心该函数的内部功能。您给它一个输入并测试返回的输出是否符合预期。这“证明”“单元”(功能)正在按预期运行,并完成了您期望的转换。

如果内部工作方式发生更改,重写甚至卸载到另一个功能,则该功能的基础应保持不变;合同不变,其输出可靠地返回您期望的结果,具体取决于输入。

如果要测试forLoop内发生的情况,可以重写它,以便forLoop的一部分是单独的函数。然后,您可以编写一个单元测试以根据其输入进行测试(如果它返回了预期的输出)。