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