2015-06-22 137 views
1

我是e2e測試的新手。我在量角器測試中遇到了問題。儘管我的努力,我的Web應用程序調用API:嘲笑服務器響應

Started GET "/api/programs" for 127.0.0.1 at 2015-06-22 10:43:19 +0200 
Processing by Api::V1::ProgramsController#index as JSON 
======= NO Authorization token ======= 

我想爲我的Web應用程序提供正確的HTTP POST響應。然而,我的代碼是不工作:

這裏是我的代碼:

describe('e2e tests', function() { 
it('FO tests', function() { 

browser.addMockModule('WebClientApp', function() { 
    console.log('test'); 
    angular.module('WebClientApp', ['ngMockE2E']) 
    .run(function($httpBackend) { 

    console.log('Test2!'); 
    alert('Test3!'); 

    $httpBackend.when('POST','api/auth/current_resource') 
     .respond(200, [{ 

      "resource": 
      {"id":"e11e5e4a-034c-4545-967a-dae395d5c950","email":"[email protected]","name":"Xaa","surname":"Xaaaa","is_active":true,"personal_number":null,"resource_name":"User","roles":"admin"} 

      "token": "AdPnyXvZZDtcPkMVE9rIDFM09WmHubAnEd4wGXLPMiPWrFu0gDH1uIg7lqXXl1k2UgmJ1ektHf3Pduq2iF0nsR3A4yJ1dw8cB2FHgw3rWMf3q4357Atg9FtC7WnHisGa" 
      }]); 

    $httpBackend.whenGET(/.*/).passThrough(); 
    }); 
}); 

browser.getRegisteredMockModules(); 
browser.get('http://0.0.0.0:9000/#/back-office/dashboard'); 

browser.pause(); 
}); 
}); 

,我有所求:

Remote Address:0.0.0.0:3000 
Request URL:http://0.0.0.0:3000/api/auth/sign_in 
Request Method:OPTIONS 
Status Code:200 OK 
Response Headers 
view source 
Access-Control-Allow-Credentials:true 
Access-Control-Allow-Headers:accept, content-type 
Access-Control-Allow-Methods:GET, PUT, DELETE, POST, OPTIONS 
Access-Control-Allow-Origin:http://localhost:9000 
Access-Control-Max-Age:1728000 
Content-Type:text/plain 
Transfer-Encoding:chunked 
X-Request-Id:1c75d93d-4539-4654-9963-a04bf45defe0 
X-Runtime:0.029612 
Request Headers 
view source 
Accept:*/* 
Accept-Encoding:gzip, deflate, sdch 
Accept-Language:pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4 
Access-Control-Request-Headers:accept, content-type 
Access-Control-Request-Method:POST 
Connection:keep-alive 
Host:0.0.0.0:3000 
Origin:http://localhost:9000 
Referer:http://localhost:9000/ 
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36 
+0

不'$ httpBackend'一個單元測試NG的場景? – maurycy

+0

它可以在e2e方法中使用:https://docs.angularjs.org/api/ngMockE2E/service/$httpBackend –

+0

它看起來像CORS問題,'請求方法:OPTIONS'你向不同的端口發出請求,它符合一個CORS,首先你做的是'OPTIONS'而不是'POST'。我的猜測是'OPTIONS'沒有給出正確的迴應,並且POST從未產生 – maurycy

回答

0

Here是一個可能的工作,如果解決該問題是CORS。 (我不相信tat $ httpBackend正在爲使用做準備)。

但是,您能否提供所調用服務的代碼:/api/programs? 也許你缺少嘲笑其他Web服務的響應,這是由一些角度工廠/服務等執行

更新:

@Dan Kanze答案,對於跨域請求您可以使用expectJSONP

Here是他的示例代碼

httpBackend.expectJSONP('http://api.stackexchange.com/2.1/users/gigablox/timeline?callback=JSON_CALLBACK') 
.respond(returnData);