我很难解决这个问题,所以任何帮助都将不胜感激。
我要做的就是在我的项目中测试一个简单的基于 Ajax 的注册表单。如果表单提交成功,您将被重定向到欢迎页面。如果不是,您会收到与每个违规字段相关的适当验证错误。
出于某种奇怪的原因,Capybara 没有遵循重定向。正在进行 Ajax 调用,我看到在数据库中注册了一个新帐户,但是onSuccess
根本没有调用回调,或者忽略了重定向。
这是我正在尝试使用的内容(为简洁起见,我已经压缩了代码):
特征:
Feature: Registration
In order to obtain a new account
As a prospective customer
I must submit a valid registration form.
@javascript
Scenario: A valid registration attempt
Given an account registration form
When I complete the form with valid values
Then I should be redirected to the welcome screen
测试:
Given(/^an account registration form$/) do
visit("/signup")
assert current_path == "/signup"
end
When(/^I complete the form with valid values$/) do
within("#signupForm") do
fill_in("email", :with => Faker::Internet.email)
fill_in("name", :with => Faker::Name.name)
fill_in("password", :with => "11111111")
click_link("signupFormSubmit")
end
end
Then(/^I should be redirected to the welcome screen$/) do
assert current_path == "/welcome"
end
JavaScript:
console.log('I am not yet inside you.')
$.post(url, form.serialize(), function(response) {
// everything went well
// let's redirect them to the given page
window.location.replace(response.redirectUrl)
console.log('I am inside you and it is good.')
}, function(response) {
// collect error responses from API
// apply error hints to associated fields
console.log('I am inside you and something went wrong.')
})
好的,所以这个特定的测试运行得很好,直到我们应该将用户重定向到欢迎屏幕。我已经尽我所能查看onSuccess
,onFailure
回调中发生了什么,但无济于事。这就像代码甚至没有被执行。
我刚刚从测试运行中得到以下输出:
Then I should be redirected to the welcome screen # features/step_definitions/registration.rb:51
Failed assertion, no message given. (MiniTest::Assertion)
./features/step_definitions/registration.rb:52:in `/^I should be redirected to the welcome screen$/'
features/registration.feature:15:in `Then I should be redirected to the welcome screen'
如果我提出异常也没关系,它不会被拾取。回调中的调用也没有console.log()
。
有人见过这个吗?如果是这样,是否有解决方法?如果您需要更多信息,请尽管询问,我将非常乐意提供。