8

我正在使用 webrat 为 Rails 应用程序编写集成测试。填写表格后,用户按下提交并创建一个帐户。

click_button "Submit"
assert_contain "Your Account Has Been Created"

但是,测试失败:

expected the following element's content to include "Your Account Has Been Created":
You are being redirected.
<false> is not true.

通常要遵循重定向,我会使用 post_via_redirect,但是从 Webrat 的示例来看,click_button 后跟 assert_contain 应该可以工作

我刚开始使用 Webrat,所以我在这里遗漏了一些明显的东西吗?为什么我被重定向响应卡住了?

谢谢!

德布

4

4 回答 4

11

使用新的 Rails 3 应用程序时,我在测试一个简单的方法时也遇到了这个问题,该方法在控制器中包含了一个 redirect_to 调用。该方法本身运行良好,但 Webrat 会返回“您正在被重定向”。回复。

在黄瓜中添加“然后显示页面”步骤(因此 webrat 看到的页面在浏览器中打开)显示“您正在被重定向。”响应带有指向 example.org 链接的链接。

基于此,我发现了 Yannimac 的补丁(http://groups.google.com/group/webrat/browse_thread/thread/fb5ff3fccd97f3df):

#/lib/webrat/core/session.rb
#starting at line 288

def current_host
- URI.parse(current_url).host || @custom_headers["Host"] || "www.example.com"
+ URI.parse(current_url).host || @custom_headers["Host"] || default_current_host
end

+ def default_current_host
+   adapter.class==Webrat::RackAdapter ? "example.org" : "www.example.com"
+ end 

进行这些更改修复了问题,因此使用 Webrat 的 redirect_to 调用现在可以正常工作。

于 2010-08-13T15:04:34.327 回答
3

rails 3 和 webrat 存在一些问题。请参见:

http://baldowl.github.com/2010/12/06/coercing-cucumber-and-webrat-to-cooperate.html

于 2011-01-30T16:44:23.970 回答
0

您的应用程序中是否有任何身份验证?我认为重定向是因为您尚未通过身份验证。如果我的假设是正确的,请编写一个设置以首先使用 Webrat 登录。

于 2010-06-04T15:38:32.897 回答
0

这是解决此问题所需要做的要点。

https://gist.github.com/752766

于 2011-02-26T01:48:10.967 回答