简体   繁体   中英

How to write Unit test cases using jamine in angular for inside code of for loop?

I want to test one function in angular using jasmine.

Here is my code,

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>`;
              }
            }
          }
        }
    }

I need to test the logic inside for loop of my function showLog(data).

How to write test cases for loop? and if case logic.

How to test this code using jasmine.

Thanks in advance

In a proper unit test, you wouldn't really care about the innerworkings of the function. You give it an input and test if the output returned is as expected. This "proves" that the "unit" (the function) is functioning as expected and doing the transformations you would expect.

If the inner workings are changes, rewritten or even offloaded to another function, the base of the function should remain the same; the contract doesn't change and it's output reliably returns what you expect depending on the input.

If you want to test what goes on within the forLoop, you could rewrite it so the part of the forLoop is a separate function. You can then write a unit test to test based on it's inputs if it returns your expected outputs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM