programing

에러가 표시되는 이유...예기치 않은 요구: GET / internalapi / quotes

mailnote 2023. 3. 27. 21:25
반응형

에러가 표시되는 이유...예기치 않은 요구: 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

반응형