2013-11-01 12 views
3

angual.module('app')module('app')有什麼區別?單元測試時的「angualar.module」和「module」的區別Angular

這裏是有問題的簡單服務和單元測試:在單元測試框架

服務

(function() { 
    "use strict" 

    var app = angular.module('app', []); 

    app.service('CustomerService', ['$http', function ($http) { 
     return { 
      getById: function (customerId) { 
       return $http.get('/Customer/' + customerId); 
      } 
     } 
    }]); 
}()); 

測試

describe('Customer Service', function() { 
    var $rootScope, 
     $httpBackend, 
     service, 
     customerId = 1; 

    beforeEach(function() { 
     angular.module('app', ['ngMock']); 

     inject(function ($injector) { 
      $rootScope = $injector.get('$rootScope'); 

      $httpBackend = $injector.get('$httpBackend'); 
      $httpBackend.whenGET('/Customer/' + customerId).respond({ id: customerId, firstName: 'Joe', lastName: 'Blow' }); 

      service = $injector.get('CustomerService'); 
     }); 
    }); 

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

    it('should get customer by id', function() { 
     var customer; 

     service.getById(1).then(function (response) { 
      customer = response.data; 
     }); 

     $httpBackend.flush(); 
     expect(customer.firstName).toBe('Sam'); 
    }); 
}); 

回答

6

module指模擬angular.mock.module方法(附加t窗戶作爲一種方便)。 angular.moduleangular.mock.module嘲笑的方法。

相關問題