2016-02-05 59 views
2

我正在編寫一個單元測試下面的控制器。我有兩個函數loadCountries和loadTimezones,我想在頁面加載時調用它。我想測試一下在頁面加載時加載的國家。在特定的測試中,我不在乎時區是否加載。所以我嘲笑時區服務。但看起來我必須爲時區模擬返回一個值。我不想明確處理它。我期待在創建SpyObj時,任何未在間諜/模擬上明確處理的函數調用都將被刪除或不起作用。如果我不連鎖returnValue,模擬正在調用真正的功能。我該如何解決 ?如何嘲笑使用茉莉花服務忽略任何電話

'use strict'; 

angular.module('nileLeApp') 
.controller('RegisterController', function ($scope, $translate, $timeout, vcRecaptchaService, Auth, Country, Timezone, RecaptchaService) { 
    $scope.success = null; 
    $scope.error = null; 
    $scope.doNotMatch = null; 
    $scope.errorUserExists = null; 
    $scope.registerAccount = {}; 
    $timeout(function() { 
     angular.element('[ng-model="registerAccount.email"]').focus(); 
    }); 

    $scope.loadCountries = function() { 
     Country.getCountries() 
      .then(function (result) { 
       $scope.countries = result.data; 
      }); 
    }; 

    $scope.loadTimezones = function() { 
     Timezone.getTimezones() 
      .then(function (result) { 
       $scope.timezones = result.data; 
      }); 
    }; 

    $scope.loadCountries(); 
    $scope.loadTimezones(); 
}); 

以下是我正在嘗試的測試。

'use strict'; 

describe('Register Controllers Tests', function() { 

describe('RegisterController', function() { 

    // actual implementations 
    var $scope; 
    var $q; 
    // mocks 
    var MockTimeout; 
    var MockTranslate; 
    var MockAuth; 
    var MockCountry; 
    var MockTimezone; 
    // local utility function 
    var createController; 

    beforeEach(inject(function ($injector) { 
     $q = $injector.get('$q'); 
     $scope = $injector.get('$rootScope').$new(); 
     MockTimeout = jasmine.createSpy('MockTimeout'); 
     MockAuth = jasmine.createSpyObj('MockAuth', ['createAccount']); 
     MockCountry = jasmine.createSpyObj('MockCountry', ['getCountries']); 
     MockTimezone = jasmine.createSpyObj('MockTimezone', ['getTimezones']); 
     MockTranslate = jasmine.createSpyObj('MockTranslate', ['use']); 


     var locals = { 
      '$scope': $scope, 
      '$translate': MockTranslate, 
      '$timeout': MockTimeout, 
      'Auth': MockAuth, 
      'Country': MockCountry, 
      'Timezone': MockTimezone 
     }; 
     createController = function() { 
      $injector.get('$controller')('RegisterController', locals); 
     }; 
    })); 

    it('should load countries on page load', function() { 

     var mockCountryResponse = {data: [{ 
      'countryId': 1, 
      'alpha2Code': "AF", 
      'countryName': "Afghanistan" 
     }]}; 

     MockCountry.getCountries.and.returnValue($q.resolve(mockCountryResponse.data)); 
     // Want to avoid explicitly specifying below line    
     MockTimezone.getTimezones.and.returnValue($q.resolve({})); 

     // given 
     createController(); 

     $scope.$apply($scope.loadCountries); 
     expect($scope.countries).toEqual(mockCountryResponse); 
    }); 

}); 

回答

0

這是不可能擺脫and.returnValue這裏,因爲控制器鏈的承諾,並期望對象Timezone.getTimezones存根將返回的方法(和它返回無)。

jasmine.createSpyObj只處理對Timezone方法的調用,而不是它們的返回值,這就是爲什麼and.returnValue在那裏。

這是完全細做

MockTimezone.getTimezones.and.returnValue($q.resolve({})); 

基於承諾,規範。