【发布时间】:2015-10-28 19:16:08
【问题描述】:
我正在测试一个单页应用程序,它向给定的 api 端点发出 GET 请求,期待一些结果。
现在,我已经使用 $httpbackend 对象模拟了 API,我需要断言正确的 URL 被传递给它(在执行 GET 请求时)。
我的 URL 包含许多 API 需要知道的额外信息(startTime、endTime 等)。我只是想测试一下是否传入了正确的东西。
这是我目前的端到端测试:
var chai = require('chai');
var chaiPromise = require("chai-as-promised");
var HttpBackend = require('http-backend-proxy');
var utils = require('../utils.js');
var expect = chai.expect;
var dateTimeSupport = require('../dateTimeFillSupport.js');
var support = require('../uhhSupport.js');
chai.use(chaiPromise);
var steps = function(){
var proxy = null;
var urlFound = "";
this.Before(function(event, callback){
proxy = new HttpBackend(browser);
callback();
});
this.After(function(event, callback){
proxy.onLoad.reset();
callback();
});
this.Given(/^my given$/, function(){
// Set up the context for the proxy - to be able to pass stuff back and forth
var simpleChartData = require('data.json');
proxy.context = {
chartData : simpleChartData,
foundUrl : urlFound
};
// Allow components and directived to pass through
proxy.onLoad.whenGET(/\.\/components\/.+/).passThrough();
proxy.onLoad.whenGET(/directives\/.+/).passThrough();
proxy.onLoad.whenGET(/.+\/api\/pvValues\/.+/).respond(function(method, url){
$httpBackend.context.foundUrl = url;
return [200, $httpBackend.context.chartData];
});
// perform action
browser.get(utils.baseUrl);
$('.dateLabel').click();
return browser.controlFlow().execute(function(){});
});
this.When(/^a card is clicked$/, function(){
return dateTimeSupport.clickTheFirstCard().then(function(){
$('.etChart').isDisplayed();
});
});
this.Then(/^the correct URL is passed to the mocked API$/, function(){
var expectedUrl = "myexpectedurl";
// here I want to check expectedUrl against $httpBackend.context.foundUrl
return browser.controlFlow().execute(function(){});
});
}
module.exports = steps;
所以问题是,如何将$httpBackend.context.foundUrl 变量传递给我的then 函数? (为了查看 ti 是否与预期的 URL 匹配?)
【问题讨论】:
标签: angularjs http protractor angularjs-e2e e2e-testing