Why do I receive error … unexpected request: GET /internalapi/quotes

前端 未结 2 1196
Happy的楠姐
Happy的楠姐 2020-12-02 21:47

I\'ve defined the following service in my angular app :

services.factory(\'MyService\', [\'Restangular\', function (Restangular) {
       return {
                   


        
相关标签:
2条回答
  • 2020-12-02 22:36

    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();
        }));
    
    });
    

    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(); 
    
    0 讨论(0)
  • 2020-12-02 22:37

    I had the same problem as you guys. My solution was to add a '/' at the start of the URL-parameter of the .expectGET. Using your example:

    $httpBackend.expectGET("/internalapi/quotes").respond({ hello: 'world'})
    

    Best of luck

    0 讨论(0)
提交回复
热议问题