量角器测试失败,但手动测试同样的东西有效(可能与 $httpBackend 相关)
Protractor test fails, but manually testing the same thing works (possibly related to $httpBackend)
好吧,如果我不使用模拟后端,我的量角器测试就可以工作,并且当我手动测试功能时(即使使用模拟后端),一切都按预期工作。唯一不起作用的是使用 $httpBackend 的量角器测试。
我有以下测试:
it("should navigate to add employee and add an employee.", function () {
var tableRowsBeforeAdd = element.all(by.repeater("employee in ec.employees")).count();
var button = element(by.className("btn"));
button.click();
expect(browser.getCurrentUrl()).toContain('add');
var jmbgInput = element(by.model("ec.employee.jmbg"));
jmbgInput.sendKeys("9988776655000");
var nameInput = element(by.model("ec.employee.name"));
nameInput.sendKeys("Test name");
var surnameInput = element(by.model("ec.employee.surname"));
surnameInput.sendKeys("Test surname");
var submit = element(by.buttonText("Save"));
submit.click();
expect(browser.getCurrentUrl()).not.toContain('add');
...
现在,浏览器打开(默认 url 指向员工列表视图),单击正确的按钮并打开员工的 add/edit 表单。它在输入框中输入正确的信息,然后单击按钮,但就在那一刻它崩溃了。上面代码末尾列出的 expect 语句,以及该语句失败后的所有其他 expect 块,因为它就像列表视图没有加载一样,根据我的服务,它应该加载:
employee.$update().then(function() {
employees.push(employee);
$location.path('/');
});
我的员工是一个 $resource 服务,我会避免列出所有其他代码,因为它与我使用真实后端以及手动使用该应用程序时的代码相同。
现在,我想添加的一件事是我 运行 在 appe2e 模块中的功能:
function appe2ePrep($httpBackend, $location) {
$httpBackend.whenGET("app/components/employee/employee-list.html").passThrough();
$httpBackend.whenGET("app/components/employee/employee-detail.html").passThrough();
var employees = [
{jmbg:"1", name: "Mirko", surname: "Ivovkić"},
{jmbg:"2", name: "Nikola", surname: "Tešić"},
{jmbg:"3", name: "Robert", surname: "Stefanović"},
{jmbg:"4", name: "Aleksa", surname: "Trifković"}
];
// All employees
$httpBackend.whenGET(dbEndpoint + "?apiKey=" + appKey).respond(employees);
// adds a new employee
$httpBackend.whenPUT(dbEndpoint + "/9988776655440?apiKey=" + appKey).respond(function (method, url, data) {
return [200];
});
}
据我所知,$location 有问题,或者至少路径有问题,因为点击后应该加载的页面没有。
好吧,很抱歉浪费了大家的时间。当我期待“9988776655000”时,我提供了字符串“9988776655440”。出于某种原因,调试器没有向我显示一个错误的请求,它所要做的就是在此处检查我的 SO post 以意识到字符串不匹配。
好吧,如果我不使用模拟后端,我的量角器测试就可以工作,并且当我手动测试功能时(即使使用模拟后端),一切都按预期工作。唯一不起作用的是使用 $httpBackend 的量角器测试。
我有以下测试:
it("should navigate to add employee and add an employee.", function () {
var tableRowsBeforeAdd = element.all(by.repeater("employee in ec.employees")).count();
var button = element(by.className("btn"));
button.click();
expect(browser.getCurrentUrl()).toContain('add');
var jmbgInput = element(by.model("ec.employee.jmbg"));
jmbgInput.sendKeys("9988776655000");
var nameInput = element(by.model("ec.employee.name"));
nameInput.sendKeys("Test name");
var surnameInput = element(by.model("ec.employee.surname"));
surnameInput.sendKeys("Test surname");
var submit = element(by.buttonText("Save"));
submit.click();
expect(browser.getCurrentUrl()).not.toContain('add');
...
现在,浏览器打开(默认 url 指向员工列表视图),单击正确的按钮并打开员工的 add/edit 表单。它在输入框中输入正确的信息,然后单击按钮,但就在那一刻它崩溃了。上面代码末尾列出的 expect 语句,以及该语句失败后的所有其他 expect 块,因为它就像列表视图没有加载一样,根据我的服务,它应该加载:
employee.$update().then(function() {
employees.push(employee);
$location.path('/');
});
我的员工是一个 $resource 服务,我会避免列出所有其他代码,因为它与我使用真实后端以及手动使用该应用程序时的代码相同。
现在,我想添加的一件事是我 运行 在 appe2e 模块中的功能:
function appe2ePrep($httpBackend, $location) {
$httpBackend.whenGET("app/components/employee/employee-list.html").passThrough();
$httpBackend.whenGET("app/components/employee/employee-detail.html").passThrough();
var employees = [
{jmbg:"1", name: "Mirko", surname: "Ivovkić"},
{jmbg:"2", name: "Nikola", surname: "Tešić"},
{jmbg:"3", name: "Robert", surname: "Stefanović"},
{jmbg:"4", name: "Aleksa", surname: "Trifković"}
];
// All employees
$httpBackend.whenGET(dbEndpoint + "?apiKey=" + appKey).respond(employees);
// adds a new employee
$httpBackend.whenPUT(dbEndpoint + "/9988776655440?apiKey=" + appKey).respond(function (method, url, data) {
return [200];
});
}
据我所知,$location 有问题,或者至少路径有问题,因为点击后应该加载的页面没有。
好吧,很抱歉浪费了大家的时间。当我期待“9988776655000”时,我提供了字符串“9988776655440”。出于某种原因,调试器没有向我显示一个错误的请求,它所要做的就是在此处检查我的 SO post 以意识到字符串不匹配。