我尝试了几种组合来测试我的工厂但总是在视图SpecRunner.html
中出错。
让我们来看看我的工厂和经过试验的单元测试。
//First service who communicated with the REST API
angular.module('testApp')
.factory('ResService', ['$resource', 'baseUrl',
function ($resource, baseUrl) {
return {
group: $resource(baseUrl + '/api/qr_group/:Id', {
Id: '@Id'
}, {})
...
//Second service who uses ResService and who will used in the controller
angular.module('testApp')
.factory('CrudService', ['ResService',
function (ResService) {
var service = {
getAllGroups: getAllGroups,
};
return service;
function getAllGroups() {
return ResService.group.query();
}
}]);
好吧,我的目标是检查服务是否称为对象或状态200.我决定为自己返回一个对象。
这里错误的代码:
describe('Services: CrudService', function () {
var CrudService,
$httpBackend,
mockData;
beforeEach(module('testApp'));
beforeEach(inject(function ($injector) {
$httpBackend = $injector.get('$httpBackend');
CrudService = $injector.get('CrudService');
mockData = {
Id: 1,
name: 'USA',
code: 'US'
};
$httpBackend.expectGET('http://localhost:63831/api/group').respond([
{
Id: 1,
name: 'USA',
code: 'US'
}
]);
}));
afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('do it', function () {
var result = CrudService.getAllGroups();
$httpBackend.flush();
expect(result[0]).toEqual(mockData);
});
我收到以下错误:
错误:[$ rootScope:inprog] http://errors.angularjs.org/1.4.6/ $ rootScope / inprog?p0 =%24digest in http://localhost:60694/js/angular.min.js(第6行)
和
错误:意外请求:GET views / groups.html没有更多请求 预期在http://localhost:60694/js/angular-mocks.js(第1244行)
我不知道问题是什么..任何人都可以帮助我吗?