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