2017-02-08 7 views
0

Ich muss hier zwei, was überprüfen:Jasmine Unit-Test-Controller

  1. Die Antwortlänge. spezifizierte die Scheindaten als [{Name: 'John', ID: 1}, {Name: 'Josh', ID: 2}], so dass die Antwortlänge 2 sein sollte. Bei diesem Test wird immer die Länge als 1 erhalten.
  2. Die Antwortdaten sollten gleich sein. dh. expect (IndexSummaryService.getIndexSummaryQueues) .toEqual ([{Name: 'John', id: 1}, {Name: 'Josh', id: 2}]);

Der Test schlägt fehl mit der Meldung: Erwartete Funktion gleich [Objekt ({name: 'John', id: 1}), Objekt ({name: 'josh', id: 2})]

Mein Service ist etwas anders, was api als Parameter annimmt, der die URL ist.

Bitte schlagen Sie vor, wie Sie diesen Komponententest durchführen können.

Dies ist der Service-Code

app.service("IndexSummaryService", ['$http', function ($http) { 
    this.getIndexSummaryQueues = function (api) {   
     return $http.get(api, { cache: false }); 
    }; 
    }]); 

Dies ist der Controller haben

$scope.loadRecords = function (api) { 
     $scope.loading = true; 
     var GetIndexSummaryQueue = IndexSummaryService.getIndexSummaryQueues(api); 
     GetIndexSummaryQueue.then(function (response) { 
      $scope.Queues = response.data; 
     }, function (error) { 
      if (error.status == 500) { 
       $scope.errorStatus = "Error " + error.status; 
       $scope.errorMsg = error.data.message; 
      } 
      else { 
       $scope.errorStatus = "Error " + error.status; 
       $scope.errorMsg = GlobalConstants.errormessage; 
      } 
      $scope.errorpage = true; 
      $scope.success = false; 
      console.log("Status Data : " + error.data.message); 
      console.log("Status Error : " + error.status); 
     }).then(function() { 
      $scope.loading = false; 
     }); 
    } 

I Unit-Test unten in Jasmin geschrieben ist der Jasmin-Code ist.

describe("ISummary ->", function() { 

beforeEach(function() { 
    module("ApplicationModule"); 
}); 

var $httpBackend; 
var scope, createController; 

beforeEach(inject(function ($rootScope, _$httpBackend_, $controller) { 
    $httpBackend = _$httpBackend_; 
    scope = $rootScope.$new(); 
    createController = function() { 
     return $controller('IndexingSummaryController', { 
      $scope: scope 
     }); 
    };  

    $httpBackend.when("GET", "https://domain.com/captivaapi/api/capturestats/pldindexingSummary") 
     .respond([{ name: 'John', id: 1 }, { name: 'Josh', id: 2 }]); 
})); 

afterEach(function() { 
    $httpBackend.verifyNoOutstandingExpectation(); 
    $httpBackend.verifyNoOutstandingRequest(); 
}); 

describe("Service->", function() { 
    it("can load topics", inject(function (IndexSummaryService) {   
     $httpBackend.expectGET("https://domain.com/captivaapi/api/capturestats/pldindexingSummary"); 
     IndexSummaryService.getIndexSummaryQueues('https://domain/captivaapi/api/capturestats/pldindexingSummary'); 
     $httpBackend.flush(); 
     expect(IndexSummaryService.getIndexSummaryQueues.length).toBeGreaterThan(0); 

     expect(IndexSummaryService.getIndexSummaryQueues.length).toEqual(2); 

     expect(IndexSummaryService.getIndexSummaryQueues).toEqual([{ name: 'John', id: 1 }, { name: 'Josh', id: 2 }]); 
    })); 
}); 

Antwort

1

Sie sind nicht die Antwort der Versprechen Prüfung, versuchen Sie die unten (unter Umständen nicht genau sein, da es eine Weile her, seit ich Angular verwendet, aber im Grunde Ihre Behauptungen in einem dann Block machen, sobald das Versprechen hat aufgelöst).

describe("Service->", function() { 
    it("can load topics", inject(function (IndexSummaryService) {   
     $httpBackend.expectGET("https://domain.com/captivaapi/api/capturestats/pldindexingSummary"); 

     IndexSummaryService.getIndexSummaryQueues('https://domain/captivaapi/api/capturestats/pldindexingSummary').then(function(res) { 
      expect(res.length).toBeGreaterThan(0); 
      expect(res.length).toEqual(2); 
      expect(res).toEqual([{ name: 'John', id: 1 }, { name: 'Josh', id: 2 }]); 
     }); 
     $httpBackend.flush(); 
    })); 
}); 
+0

Danke Lee..es hat funktioniert..eine kleine Änderung, die hinzugefügt wurde..res.data.length und res.data .. –