Capybara 和 Rails,为什么在规范中使用 should have_content 会给我“你正在被重定向。”?
我正在 Rspec 中使用 Capybara 为 Rails 应用程序编写集成测试。填写完表单后,用户按下提交。
如果我运行测试,我会得到:
预计“您是 被重定向。”
我正在尝试测试结果页面内容,这是测试:
describe PagesController do
describe "Quiz testing in chapter" do
def page_view
Capybara::Node::Simple.new(@response.body)
end
render_views
login_student
it "should fail if user chosen wrong answer" do
page= create_quiz_page_with_two_choices_first_correct
post :answer_quiz, :page_id=>page.id, :submitted_single_answer=>'2'
page_view.should have_content("Welcome to course builder")
end
end
end
在 PagesController
中
def answer_quiz
...
respond_to do |format|
format.html { redirect_to page }
format.json { head :ok }
end
end
,我刚刚开始使用 Capybara,所以我在这里遗漏了一些明显的东西吗?为什么我坚持重定向 ?
希望
回复
I'm writing an integration test for a rails application using Capybara within Rspec. After filling out a form, the user presses submit.
If I run the test I get:
expected there to be content "Welcome to course builder" in "You are
being redirected."
I am trying to test the resulted page content, here is the test:
describe PagesController do
describe "Quiz testing in chapter" do
def page_view
Capybara::Node::Simple.new(@response.body)
end
render_views
login_student
it "should fail if user chosen wrong answer" do
page= create_quiz_page_with_two_choices_first_correct
post :answer_quiz, :page_id=>page.id, :submitted_single_answer=>'2'
page_view.should have_content("Welcome to course builder")
end
end
end
in the PagesController
def answer_quiz
...
respond_to do |format|
format.html { redirect_to page }
format.json { head :ok }
end
end
I just started using Capybara, so am I missing something obvious here? Why am I stuck with the redirect response?
Thanks!
hopewise
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
login_student
需要进入before
块:但是即使这样也可能无法解决问题。使用 Capybara 时,建议通过访问每个示例的登录页面来实际登录,因此:
编写一个特殊案例来处理测试环境中的登录也很有帮助,这样您就可以避免为测试登录创建用户。
login_student
needs to go in abefore
block:However even that probably won't fix the problem. When using Capybara it is recommended to actually login by visiting the login page for each example, so:
It's also helpful to write a special case to handle logins in the test environment, so you can avoid having to create a user for test logins.