2014-10-02 78 views
0

我是新來的angularjs測試,並且一直在嘗試運行這個測試,但是它一次又一次地失敗並且出現相同的錯誤。我在這裏查看問題並閱讀文檔,但沒有得到這個錯誤的原因。角單元測試失敗,錯誤:意外的請求:GET

幫助將不勝感激。提前致謝。

我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'); 

    })); 

    }); 

首測(當我如果它被定義通過測試),但下一個失敗。

錯誤:

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) 

燒瓶服務器在另一終端中運行。 請讓我知道我在這裏做錯了什麼,以及如何進一步處理。

+0

當您將網址更改爲'var url ='/ api/apps/a365cc3520c7a70a553e95ee354670264''時會發生什麼? – 2014-10-02 17:59:55

+0

是的,那算出來了。通過添加整個地址我不知道我在想什麼。 – anurag619 2014-10-06 06:58:09

回答

1

正如在評論中提到的那樣,改變了網址。我還更改了spec文件中的新行。希望這可以幫助其他人。

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

    })); 

    }); 
+1

嗨,有沒有一種方法使$絕對路徑使用$資源工作單元測試?我使用絕對路徑在我的服務中調用$資源,因爲是跨域調用。我試了幾天,但結果是相同的「意外的請求:GET」。有沒有人成功,我看到這是簡單的$ http正常工作,但沒有$資源?謝謝! – Webdesigner 2015-06-14 05:55:47

相關問題