找到你要的答案

Q:Testing AngularJS resource with Jasmine

Q:测试AngularJS资源与贾斯敏

I would like to test my resource with following URL:

    $resource(API_URL+'items/:id', {});

Where API_URL equals /app/api/

describe('item',function(){
    it('should return same item id',function(){ 
        $httpBackend.expectGET(API_URL+'items/1').respond({id:1});
        var result = ItemService.item.get({id:1});
        $httpBackend.flush();
        expect(result.id).toEqual(1);
    });
});

But the problem is that this test fails for some strange reason:

    Error: Unexpected request: GET modules/home/partials/home.html
    No more request expected
        at $httpBackend 

And when I add slash to $httpBackend URL like this:

    $httpBackend.expectGET(API_URL+'/items/1').respond({id:1});

It throws following expection:

    Error: Unexpected request: GET /app/api/items/1
    Expected GET /app/api//items/1
        at $httpBackend 

Note double slash in expected GET.

How to fix it?

Update: Code from ItemService:

    var itemService = angular.module("ItemServiceModule", []);
    itemService.factory("ItemService", [ "$resource", "API_URL", function($resource,API_URL) {
        var itemService = {
            item: $resource(API_URL+'items/:id', {})
        };
        return itemService;
    }]);

Update2: If I change httpBackend url like this (just add localhost and not adding slash before items):

      $httpBackend.expectGET('http://localhost:8080'+API_URL+'items/1').respond({id:1});

Then the error is:

    Error: Unexpected request: GET /app/api/items/1
    Expected GET http://localhost:8080/app/api/items/1
        at $httpBackend 

Update3: If I for example hard code that API_URL like this:

 var url = 'app/api/items/1';
        $httpBackend.expectGET(url).respond({id:1});

The error is:

 Error: Unexpected request: GET /app/api/items/1
    Expected GET app/api/items/1
        at $httpBackend

But when I add slash at the begining of url, then it requests home partial modules/home/partials/home.html as with API_URL.

    Error: Unexpected request: GET modules/home/partials/home.html
    No more request expected
        at $httpBackend 

我想测试我的资源与以下网址:

    $resource(API_URL+'items/:id', {});

在api_url等于/应用程序/接口/

describe('item',function(){
    it('should return same item id',function(){ 
        $httpBackend.expectGET(API_URL+'items/1').respond({id:1});
        var result = ItemService.item.get({id:1});
        $httpBackend.flush();
        expect(result.id).toEqual(1);
    });
});

但问题是,这个测试失败的一些奇怪的原因:

    Error: Unexpected request: GET modules/home/partials/home.html
    No more request expected
        at $httpBackend 

当我添加斜线httpbackend美元这样的URL:

    $httpBackend.expectGET(API_URL+'/items/1').respond({id:1});

把下面的期望:

    Error: Unexpected request: GET /app/api/items/1
    Expected GET /app/api//items/1
        at $httpBackend 

注意双倍斜线在预期得到。

如何修复它?

Update: Code from ItemService:

    var itemService = angular.module("ItemServiceModule", []);
    itemService.factory("ItemService", [ "$resource", "API_URL", function($resource,API_URL) {
        var itemService = {
            item: $resource(API_URL+'items/:id', {})
        };
        return itemService;
    }]);

Update2: If I change httpBackend url like this (just add localhost and not adding slash before items):

      $httpBackend.expectGET('http://localhost:8080'+API_URL+'items/1').respond({id:1});

然后错误是:

    Error: Unexpected request: GET /app/api/items/1
    Expected GET http://localhost:8080/app/api/items/1
        at $httpBackend 

Update3: If I for example hard code that API_URL like this:

 var url = 'app/api/items/1';
        $httpBackend.expectGET(url).respond({id:1});

错误是:

 Error: Unexpected request: GET /app/api/items/1
    Expected GET app/api/items/1
        at $httpBackend

但当我添加在URL开始削减,然后要求国内部分模块/家/部分/ home.html与api_url。

    Error: Unexpected request: GET modules/home/partials/home.html
    No more request expected
        at $httpBackend 
answer1: 回答1:

It turned out that the problem was with ui-router. Namely, the following configuration:

$urlRouterProvider.otherwise("/");

I just needed to eliminate this configuration in the tests:

beforeEach(module(function ($urlRouterProvider) {
    $urlRouterProvider.otherwise(function(){return false;});
}));

Found this solution on https://github.com/angular-ui/ui-router/issues/212

原来,问题是与UI路由器。即以下配置:

$urlRouterProvider.otherwise("/");

我只是需要在测试中消除这种配置:

beforeEach(module(function ($urlRouterProvider) {
    $urlRouterProvider.otherwise(function(){return false;});
}));

在https://github.com/angular-ui/ui-router/issues/212发现这个解决方案

angularjs  testing  jasmine  angular-ui-router  angular-resource