Home AngularJS: mocking the service in a controller using Jasmine
Reply: 1

AngularJS: mocking the service in a controller using Jasmine

aman jain
1#
aman jain Published in 2018-02-13 18:05:03Z

Could you please help me to write down the Jasmine(2.0) test code for mock of the service in a Controller as below.

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) { 
      //  
    }) 
  }); 

The Controller file readjson.js is as below.

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
                }
              ]
            }
          ]
        }
       ]
      }
     ]
    }

Please post an apporopriate spac.js file. Thanks in advance.

Bulkan
2#
Bulkan Reply to 2018-02-13 23:05:39Z

You do use the following to mock your service

beforeEach(function() {
  angular.mock.module('myAssignmentTaskApp', ($provide) => {
    const mockReadJsonFactory = {
    };
    $provide.constant('readJsonFactory', mockReadJsonFactory);
  });
});
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.389613 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO