anurag619
anurag619

Reputation: 712

Angular Unit test fails with Error: Unexpected request: GET

I am new to angularjs testing and have been trying to run this test but it fails with same error again and again. I have viewed questions here and read the docs but haven't got to the cause of this error.

A help will be greatly appreciated. Thanks in advance.

my service.js

'use strict';

var services = angular.module('services',['ngResource'])

services.factory('callAppsList',['$resource',function($resource){
    return $resource('/api/apps/:appId', {}, {
        query: {method:'GET', isArray:false},
        get: {method:'GET', isArray:false},
    });
}])

serviceSpec.js

//serviceSpec testing

describe("Testing service", function() {

    beforeEach(module('services'));

    var service, $httpBackend, response;
    var url = 'http://0.0.0.0:5000/api/apps/a365cc3520c7a70a553e95ee354670264'


     beforeEach(inject(function( _$httpBackend_, callAppsList) {
        $httpBackend = _$httpBackend_;
        res = { msg :'name'};

        service = callAppsList;

    }));

    afterEach(function() {
      $httpBackend.verifyNoOutstandingExpectation();
      $httpBackend.verifyNoOutstandingRequest();
    });

    //tests

    it("callAppsList should be defined", function () {
        expect(service).toBeDefined();
    });


    it('should send a ping and return the response',(function () {

        res = service.get({appId: 'a365cc3520c7a70a553e95ee354670264'});
        $httpBackend.whenGET(url).respond(res);

        $httpBackend.expectGET(url)

        $httpBackend.flush();
        //expect(res.msg).toEqual('name');

    }));

  });

the first test (when I am testing if it is defined passes) but the next one fails.

error :

Error: Unexpected request: GET /api/apps/a365cc3520c7a70a553e95ee354670264
    Expected GET http://0.0.0.0:5000/api/apps/a365cc3520c7a70a553e95ee354670264 in /home/anurag/anurag/projects/betablide/applunge/glide/static/test/lib/angular-mocks.js (line 1179)

A flask server is running in another terminal. Please let me know what I am doing wrong here and how to proceed further.

Upvotes: 0

Views: 4661

Answers (1)

anurag619
anurag619

Reputation: 712

As mentioned in the comments, changing the url worked out. I have also changed new lines in the spec file. Hope this may help others.

//serviceSpec testing

describe("Testing service", function() {

    beforeEach(module('services'));

    var service, $httpBackend, response;
    var url = '/api/apps/a365cc3520c7a70a553e95ee354670264'


     beforeEach(inject(function( _$httpBackend_, callAppsList) {
        $httpBackend = _$httpBackend_;    
        service = callAppsList;

    }));

    afterEach(function() {
      $httpBackend.verifyNoOutstandingExpectation();
      $httpBackend.verifyNoOutstandingRequest();
    });

    //tests

    it("callAppsList should be defined", function () {
        expect(service).toBeDefined();
    });


    it('should send a ping and return the response',(function () {

        res = service.get({appId: 'a365cc3520c7a70a553e95ee354670264'});
        $httpBackend.whenGET(url).respond({status: 200});
        //explicitly flushes pending requests
        $httpBackend.flush();
        expect(res.status).toEqual(200);

    }));

  });

Upvotes: 1

Related Questions