AngularJS:使用Jasmine

时间:2018-02-13 18:05:04

标签: angularjs unit-testing jasmine

请你帮我写一下Jasmine(2.0)测试代码,用于在Controller中模拟服务,如下所示。

readJsonFactory.js

angular.module('myAssignmentTaskApp')
  .factory('readJsonFactory', function ($http) {
    var userExists = false; 
    var responseData = []; 
    return $http.get('../json/AutoTestDB1.json').then(function (response) { 
          for (var i=0; i<response.data.StatusTable.length; i++){
            responseData.push(response.data.StatusTable[i].RunId);
          }
          return response; 
    }).catch(function (error) { 
      //  
    }) 
  }); 

Controller文件readjson.js如下所示。

angular.module('myAssignmentTaskApp')
  .controller('ReadjsonctrlCtrl', function ($scope,readJsonFactory,$location) { 
var testCaseNameFromReadJsonFactory = []; 
readJsonFactory.then(function (response) { 
for (`var i=0;i<response.data.StatusTable.length;i++`){ 
testCaseNameFromReadJsonFactory.push(response.data.StatusTable[i].TestScenario);
        }
      }
})

AutoTestDB1.json

{
  "StatusTable": [
    {
      "RunId": "bah_regression_alternateFlights",
      "TestScenario": "BAH - Change Default Search Options",
      "Area": "Yes",
      "TestCases": [
        {
          "TestID": "",
          "TestName": "VerifyCarDepotPageIsDisplayed_Test",
          "Status": [
            {
              "Release": " R301",
              "Runner": "yes",
              "Status": "Passed",
              "details": [
                {
                  "ResponseTime": "1m 26s 702ms",
                  "Status": "Passed",
                  "RecordData": 1511519114413
                }
              ]
            }
          ]
        }
       ]
      }
     ]
    }

请发布一个apporopriate spac.js文件。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

您确实使用以下内容来模拟您的服务

beforeEach(function() {
  angular.mock.module('myAssignmentTaskApp', ($provide) => {
    const mockReadJsonFactory = {
    };
    $provide.constant('readJsonFactory', mockReadJsonFactory);
  });
});