2017-02-08 7 views
0

мне нужно проверить две вещи здесь:жасмин модульного тестирования контроллера

  1. Длина ответа. указали макетные данные как [{name: 'John', id: 1}, {name: 'Josh', id: 2}], поэтому длина ответа должна быть равна 2. он не выполняет этот тест, всегда получая длину как 1.
  2. Данные ответа должны быть равны. то есть. ожидать (IndexSummaryService.getIndexSummaryQueues) .toEqual ([{name: 'John', id: 1}, {name: 'Josh', id: 2}]);

Тест терпит неудачу с Message: Ожидаемая функция равного [объекта ({имя: 'Джон', ID: 1}), объект ({имя: 'Josh', ID: 2})]

Моя служба бит отличается от другой, которая принимает api как параметр, который является URL-адресом.

Просьба предложить, как сделать этот модульный тест работоспособным.

Это код услуги

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

Это контроллер

$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; 
     }); 
    } 

Я написал модульное тестирование в жасмине ниже код жасмина.

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

ответ

1

Вы не тестируете ответ на обещании, попробуй ниже (не может быть точным, как это было некоторое время, так как я использовал Угловой, но в основном делают ваши утверждения в то блок после того, как обещание имеет разрешен).

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

Thank you Lee..it работал..а незначительные изменения, которые были добавлены..res.data.length и res.data .. –

 Смежные вопросы

  • Нет связанных вопросов^_^