使用$ httpBackend时出现Jasmine错误

时间:2014-11-11 14:54:05

标签: angularjs unit-testing jasmine httpbackend

我在Visual Studio中使用Jasmine,在 AngularJs 之上使用chutzpah测试适配器。

运行测试时出现此错误:

  • 错误:意外请求:GET / webapi / api / Map
  • 错误:请求不满意:GET / webapi / api / Map /

服务:

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

试验:

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

任何人都可以提出我失踪的内容吗?

1 个答案:

答案 0 :(得分:0)

错误发生在网址:

刚刚删除尾部斜杠,它工作正常。 $httpBackend.when('GET', '/webapi/api/Map')