반응형
에러가 표시되는 이유...예기치 않은 요구: GET / internalapi / quotes
각진 앱에서 다음과 같은 서비스를 정의했습니다.
services.factory('MyService', ['Restangular', function (Restangular) {
return {
events : { loading : true },
retrieveQuotes : function() {
return Restangular.all('quotes').getList().then(function() {
return { hello: 'World' };
});
}
};
}]);
테스트하기 위해 다음과 같은 스펙을 쓰고 있습니다.
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();
});
});
테스트를 실행할 때마다 첫 번째 테스트는 통과하지만 두 번째 테스트에서는 다음 오류가 발생합니다.
Error: Unexpected request: GET /internalapi/quotes
내가 뭘 잘못하고 있지?
편집:
알고보니, 이 구성에는Restangular
이렇게... RestangularProvider.setBaseUrl("/internalapi");
하지만 난 가짜 전화였어internalapi/quotes
. "/"가 없는 것에 주목하십시오.제가 슬래시를 넣었을 때/internalapi/quotes
모두 양호:)
GET 요청을 예상하려면 $httpBackend에 알려야 합니다.
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();
}));
});
다른 방법으로,respond()
에expectGET()
나는 내 옷을 입는 것을 선호한다.whenGET()
의 스테이트먼트beforeEach()
이렇게 하면 모든 테스트에서 반응을 정의할 필요가 없습니다.
//expect a get request to "internalapi/quotes"
$httpBackend.expectGET("internalapi/quotes").respond({ hello: 'World' });
ms.retrieveQuotes();
$httpBackend.flush();
나도 너희와 같은 문제가 있었어.해결책은 .expectGET의 URL 파라미터 시작 부분에 '/'를 추가하는 것이었습니다.예를 들어 다음과 같습니다.
$httpBackend.expectGET("/internalapi/quotes").respond({ hello: 'world'})
행운을 빌어요.
언급URL : https://stackoverflow.com/questions/18147606/why-do-i-receive-error-unexpected-request-get-internalapi-quotes
반응형
'programing' 카테고리의 다른 글
PropType과흐름 (0) | 2023.03.27 |
---|---|
JavaScript의 Gutenberg 에디터 콘텐츠(WordPress) (0) | 2023.03.27 |
npm @types org 패키지의 TypeScript 입력 (0) | 2023.03.27 |
PHP를 사용하여 REST API 만들기 (0) | 2023.03.27 |
Oracle SQL의 BLOB에서 텍스트 콘텐츠를 가져오려면 어떻게 해야 합니까? (0) | 2023.03.27 |