Jasmine error when using $httpBackend

385 views Asked by At

I'm using Jasmine in Visual Studio with chutzpah test adapter on top of AngularJs.

I get this error when running the test:

  • Error: Unexpected request: GET /webapi/api/Map
  • Error: Unsatisfied requests: GET /webapi/api/Map/

service:

var services = angular.module('mapService', ['ngResource']);
services.factory('mapService', ['$resource', 
function ($resource) {
    var objects = undefined;
    var res = $resource('/webapi/api/Map/', {}, {
        query: {
            method: 'GET',
            isArray: true
        }
    });
    return {
        getObjects: function (callback) {
            res.query(function(successResult) {
                objects = successResult;
            });
            return objects;
        }
    }
}]);

test:

describe('testing the department controller', function () {
    var $scope, $location, mapController, $httpBackend, myMapService;

    beforeEach(module('app'));

    beforeEach(inject(function ($injector, $rootScope, $controller, _$httpBackend_, mapService) {
        $httpBackend = _$httpBackend_;
        $httpBackend.when('GET', '/webapi/api/Map/')
            .respond([jasonArray]);

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

    it('should have objects', function () {
        $httpBackend.expect('GET', '/webapi/api/Map/');

        var objects = myMapService.getObjects(function (result) {
            objects = result;
        });
        $httpBackend.flush();
        expect(objects.length).toBeGreaterThan(0);
    });
});

Can anyone suggest what I'm missing?

1

There are 1 answers

0
Andre On BEST ANSWER

The error was in the url:

Just removed the trailing slash and it worked fine. $httpBackend.when('GET', '/webapi/api/Map')