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":"admin@aa.com","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
4

1 回答 1

0

如果问题是 CORS,这是一个可能的解决方法。(我不相信 tat $httpBackend 是为这种用途做准备的)。

但是您能否提供正在调用的服务的代码:/api/programs?也许您缺少模拟其他 Web 服务响应,这些响应由一些 Angular 工厂/服务等执行。

更新:

@Dan Kanze回答说,对于跨域请求,您可以使用expectJSONP

是他的示例代码

httpBackend.expectJSONP('http://api.stackexchange.com/2.1/users/gigablox/timeline?callback=JSON_CALLBACK')
.respond(returnData);
于 2015-08-17T16:18:40.857 回答