我想测试angularjs资源。
'use strict';
/**
* AddressService provides functionality to use address resource in easy way.
*
* This is an example usage of method:
*
* `get`:
*
var a = AddressService.get({id: '1'},
function (data) {
// Work here with your resource
}
);
*
*/
App.factory('AddressService', function ($resource, $rootScope) {
var url = [
$rootScope.GLOBALS.API_PATH,
$rootScope.GLOBALS.API_VERSION,
'models/address/:id'
].join('/'),
actions = {
'get': {
method: 'GET',
params: {id: '@id'}
}
};
return $resource(url, {}, actions);
});
我创建了测试:
'use strict';
var $httpBackend;
describe('Service: AddressService', function () {
beforeEach(module('myApp'));
beforeEach(inject(function ($injector) {
var url_get = 'api/v1/models/address/5';
var response_get = {
address_line_1: '8 Austin House Netly Road',
address_line_2: 'Ilford IR2 7ND'
};
$httpBackend = $injector.get('$httpBackend');
$httpBackend.whenGET(url_get).respond(response_get);
}));
describe('AddressService get test', function () {
it('Tests get address', inject(function (AddressService) {
var address = AddressService.get({ id: '5'});
$httpBackend.flush();
expect(address.address_line_1).toEqual('8 Austin House Netly Road');
expect(address.address_line_2).toEqual('Ilford IR2 7ND');
}));
});
});
我没有很好的角度经验。 我在karma.config.js中设置了jasmine。 AngularJS v1.0.6 Yoeman和Grunt管理项目。
我尝试grunt test
Running "karma:unit" (karma) task
INFO [karma]: Karma server started at http://localhost:8080/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 27.0 (Linux)]: Connected on socket id RFFUY5bW8Hb5eTu0n-8L
Chrome 27.0 (Linux) Service: AddressService AddressService get Tests get address FAILED
Error: Unexpected request: GET views/home.html
No more request expected
at Error (<anonymous>)
at $httpBackend (/home/bart/y/projects/x/frontend/app/components/angular-mocks/angular-mocks.js:910:9)
at sendReq (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:9087:9)
at $http (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:8878:17)
at Function.$http.(anonymous function) [as get] (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:9021:18)
at $q.when.then.then.next.locals (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:7394:34)
at wrappedCallback (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:6797:59)
at wrappedCallback (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:6797:59)
at /home/bart/y/projects/x/frontend/app/components/angular/angular.js:6834:26
at Object.Scope.$eval (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:8011:28)
Error: Declaration Location
at window.jasmine.window.inject.angular.mock.inject (/home/bart/y/projects/x/frontend/app/components/angular-mocks/angular-mocks.js:1744:25)
at null.<anonymous> (/home/bart/y/projects/x/frontend/test/spec/services/userdata/AddressService.js:32:30)
at null.<anonymous> (/home/bart/y/projects/x/frontend/test/spec/services/userdata/AddressService.js:31:5)
at /home/bart/y/projects/x/frontend/test/spec/services/userdata/AddressService.js:5:1
..................................................................
Chrome 27.0 (Linux): Executed 67 of 67 (1 FAILED) (0.343 secs / 0.179 secs)
Warning: Task "karma:unit" failed. Use --force to continue.
如果我在测试中删除了$httpBackend.flush()
。测试正在过去。但是我从undefined
获得address
。我看到了一些示例:example都使用了flush()
但只有我得到了愚蠢的例外:Error: Unexpected request: GET views/home.html
views/home.html
是我在项目目录中的视图。我不知道如何解决我的问题,我找不到任何解决方案。我不知何故错过了这一点?
有人可以在我的代码中看到错误吗?所有建议都将受到赞赏。
编辑
我发现我需要使用它:
$httpBackend.when('GET', /\.html$/).passThrough();
但另一个问题是我得到了:
TypeError: Object #<Object> has no method 'passThrough'
答案 0 :(得分:8)
我终于找到了解决方案:
我试图遵循这个:other post
所以我添加了$templateCache
:
'use strict';
var $httpBackend;
describe('Service: AddressService', function () {
beforeEach(module('myApp'));
beforeEach(inject(function ($injector, $templateChache) {
$templateCache.put('views/home.html', '.<template-goes-here />');
var url_get = 'api/v1/models/address/5';
var response_get = {
address_line_1: '8 Austin House Netly Road',
address_line_2: 'Ilford IR2 7ND'
};
$httpBackend = $injector.get('$httpBackend');
$httpBackend.whenGET(url_get).respond(response_get);
}));
describe('AddressService get test', function () {
it('Tests get address', inject(function (AddressService) {
var address = AddressService.get({ id: '5'});
$httpBackend.flush();
expect(address.address_line_1).toEqual('8 Austin House Netly Road');
expect(address.address_line_2).toEqual('Ilford IR2 7ND');
}));
});
});
答案 1 :(得分:8)
错误是由于您的测试尝试加载应用的主页而导致的。由于您尚未告知测试期望此服务器调用,因此会返回错误。有关此问题的说明,请参阅$httpBackend的文档。
您的$ templateCache解决方法专为单元测试指令而设计。你非常接近:
$httpBackend.when('GET', /\.html$/).passThrough();
由于您不需要对实际模板文件执行任何操作,因此将其添加到beforeEach()块是一种安全而简单的方法。
$httpBackend.whenGET(/\.html$/).respond('');
这可以阻止你获得你的TypeError。
TypeError: Object #<Object> has no method 'passThrough'
升级到新版本的Angular JS并使用respond('')
工作正常后,我的单元测试中出现了同样的问题。