模拟服务器响应
Mocking a server response
我是端到端测试的新手。我在量角器测试中遇到了问题。尽管我很努力,但我的 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
如果问题是 CORS,Here 是一种可能的解决方法。 (我不相信 tat $httpBackend 是为这种用途做准备的)。
但是你能提供正在调用的服务的代码吗:/api/programs
?
也许您缺少模拟其他 Web 服务响应,这些响应由一些 angular factory/service 等
执行
更新:
@Dan Kanze answer that for cross domain request you can use expectJSONP。
Here是他的示例代码
httpBackend.expectJSONP('http://api.stackexchange.com/2.1/users/gigablox/timeline?callback=JSON_CALLBACK')
.respond(returnData);
我是端到端测试的新手。我在量角器测试中遇到了问题。尽管我很努力,但我的 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
Here 是一种可能的解决方法。 (我不相信 tat $httpBackend 是为这种用途做准备的)。
但是你能提供正在调用的服务的代码吗:/api/programs
?
也许您缺少模拟其他 Web 服务响应,这些响应由一些 angular factory/service 等
更新:
@Dan Kanze answer that for cross domain request you can use expectJSONP。
Here是他的示例代码
httpBackend.expectJSONP('http://api.stackexchange.com/2.1/users/gigablox/timeline?callback=JSON_CALLBACK')
.respond(returnData);