测试与外部服务角$资源 [英] Testing Angular $resource with external service
问题描述
我努力让自己在REST请求,我采用了棱角分明$资源做一些基本的测试。
该服务code的作品就好。
I'm trying to make some basic tests on REST requests I'm doing using Angular $resource. The service code works just fine.
'use strict';
angular.module('lelylan.services', ['ngResource']).
factory('Device', ['Settings', '$resource', '$http', function(Settings, $resource, $http) {
var token = 'df39d56eaa83cf94ef546cebdfb31241327e62f8712ddc4fad0297e8de746f62';
$http.defaults.headers.common["Authorization"] = 'Bearer ' + token;
var resource = $resource(
'http://localhost:port/devices/:id',
{ port: ':3001', id: '@id' },
{ update: { method: 'PUT' } }
);
return resource;
}]);
我使用的是指令内的设备资源和它的作品。问题出来
当我开始制作上的服务的一些测试。这里有一个简单的测试,我嘲笑
HTTP请求使用$ httpBackend,我做出了嘲笑URL的请求。
I'm using the Device resource inside a directive and it works. The problems comes out when I start making some tests on the services. Here is a sample test where I mock the HTTP request using $httpBackend and I make a request to the mocked URL.
不幸它不返回任何东西,虽然请求。我敢肯定,这个
因为如果到另一个URL的请求时,测试套件会自动引发错误。
我已经花了很多时间,但没有解决方案。这里测试code。
Unluckily it does not return anything, although the request is made. I'm sure about this because if a request to another URL is made, the test suite automatically raises an error. I've been spending lot of time, but no solutions. Here the test code.
'use strict';
var $httpBackend;
describe('Services', function() {
beforeEach(module('lelylan'));
beforeEach(inject(function($injector) {
var uri = 'http://localhost:3001/devices/50c61ff1d033a9b610000001';
var device = { name: 'Light', updated_at: '2012-12-20T18:40:19Z' };
$httpBackend = $injector.get('$httpBackend');
$httpBackend.whenGET(uri).respond(device)
}));
describe('Device#get', function() {
it('returns a JSON', inject(function(Device) {
device = Device.get({ id: '50c61ff1d033a9b610000001' });
expect(device.name).toEqual('Light');
}));
});
});
由于设备不加载,这是错误
As the device is not loaded this is the error.
Expected undefined to equal 'Light'.
Error: Expected undefined to equal 'Light'.
我试过也使用下面的解决方案,但它不进入功能
检查预期。
I've tried also using the following solution, but it doesn't get into the function to check the expectation.
it('returns a JSON', inject(function(Device) {
device = Device.get({ id: '50c61ff1d033a9b610000001' }, function() {
expect(device.name).toEqual('Light');
});
}));
任何建议或链接来解决这个问题真的AP preciated。
非常感谢。
Any suggestion or link to solve this problem is really appreciated. Thanks a lot.
推荐答案
您是非常接近的,唯一缺少的就是给呼叫<code> $ httpBackend.flush(); 。工作测试看起来如下:
You were very close, the only thing missing was a call to the $httpBackend.flush();
. The working test looks like follows:
it('returns a JSON', inject(function(Device) {
var device = Device.get({ id: '50c61ff1d033a9b610000001' });
$httpBackend.flush();
expect(device.name).toEqual('Light');
}));
和现场试验plunker:<一href=\"http://plnkr.co/edit/Pp0LbLHs0Qxlgqkl948l?p=$p$pview\">http://plnkr.co/edit/Pp0LbLHs0Qxlgqkl948l?p=$p$pview
and a live test in plunker: http://plnkr.co/edit/Pp0LbLHs0Qxlgqkl948l?p=preview
您可能还需要检查$ httpBackend模拟 文档。
You might also want to check docs for the $httpBackend mock.
这篇关于测试与外部服务角$资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!