为什么我收到错误...意想不到的请求:GET / internalapi /报价 [英] Why do I receive error ... unexpected request: GET /internalapi/quotes
问题描述
我在我的角度定义的应用程序的以下服务:
I've defined the following service in my angular app :
services.factory('MyService', ['Restangular', function (Restangular) {
return {
events : { loading : true },
retrieveQuotes : function() {
return Restangular.all('quotes').getList().then(function() {
return { hello: 'World' };
});
}
};
}]);
和我写了下面的规范进行测试:
and I'm writing the following spec to test it :
describe("MyService", function () {
beforeEach(module('MyApp'));
beforeEach(module("restangular"));
var $httpBackend, Restangular, ms;
beforeEach(inject(function (_$httpBackend_, _Restangular_, MyService) {
ms = MyService;
$httpBackend = _$httpBackend_;
Restangular = _Restangular_;
}));
it("retrieveQuotes should be defined", function () {
expect(ms.retrieveQuotes).toBeDefined();
});
it("retrieveQuotes should return array of quotes", function () {
$httpBackend.whenGET("internalapi/quotes").respond({ hello: 'World' });
ms.retrieveQuotes();
$httpBackend.flush();
});
});
每当运行测试,第一测试通过,但是所述第二测试产生错误:
Whenever I run the tests, the first test passes but the second test produces the error :
错误:意外的请求:GET / internalapi /报价
我在做什么错了?
编辑:
原来我已经配置的 Restangular
是这样的... RestangularProvider.setBaseUrl(/ internalapi);
。但我要伪装 internalapi /报价
来电。请注意缺少/的。有一次,我加斜杠 / internalapi /报价
一切都很好:)
It turned out I'd configured Restangular
like so ... RestangularProvider.setBaseUrl("/internalapi");
. But I was faking calls to internalapi/quotes
. Notice the lack of the "/". Once I added the slash /internalapi/quotes
all was good :)
推荐答案
您需要告诉$ httpBackend期待一个GET请求。
You need to tell $httpBackend to expect a GET request.
describe("MyService", function () {
beforeEach(module('MyApp'));
beforeEach(module("restangular"));
var Restangular, ms;
beforeEach(inject(function (_Restangular_, MyService) {
ms = MyService;
Restangular = _Restangular_;
}));
it("retrieveQuotes should be defined", function () {
expect(ms.retrieveQuotes).toBeDefined();
});
it("retrieveQuotes should return array of quotes", inject(function ($httpBackend) {
$httpBackend.whenGET("internalapi/quotes").respond({ hello: 'World' });
//expect a get request to "internalapi/quotes"
$httpBackend.expectGET("internalapi/quotes");
ms.retrieveQuotes();
$httpBackend.flush();
}));
});
另外,你可以把你的响应()
在 expectGET()
。我preFER把我的 whenGET()
语句在 beforeEach()
这样我就不必定义每个测试中的反应。
Alternatively you can put your respond()
on your expectGET()
. I prefer to put my whenGET()
statements in a beforeEach()
that way I do not have to define the response within every test.
//expect a get request to "internalapi/quotes"
$httpBackend.expectGET("internalapi/quotes").respond({ hello: 'World' });
ms.retrieveQuotes();
$httpBackend.flush();
这篇关于为什么我收到错误...意想不到的请求:GET / internalapi /报价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!