Webrat 和 Rails:在 click_button 之后使用 assert_contain 会给我“您正在被重定向”

发布于 2024-09-04 02:01:23 字数 512 浏览 5 评论 0原文

我正在使用 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,所以我在这里遗漏了一些明显的东西吗?为什么我被重定向响应困住了?

谢谢!

德布

I'm writing an integration test for a rails application using webrat. After filling out a form, the user presses submit and an account is created.

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

However, the test fails:

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

Normally to follow a redirect I would use post_via_redirect, but from just looking at Webrat's examples, click_button followed by assert_contain should work

I just started using Webrat, so am I missing something obvious here? Why am I stuck with the redirect response?

Thanks!

Deb

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逐鹿 2024-09-11 02:01:24

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

Do you have any authentication in your apps? I presume the redirection is because of you have not been authenticated. If my assumption is right, write a setup to login first with Webrat.

a√萤火虫的光℡ 2024-09-11 02:01:24

以下是解决此问题所需执行的要点。

https://gist.github.com/752766

Here is the gist with exactly what you need to do to solve this problem.

https://gist.github.com/752766

在巴黎塔顶看东京樱花 2024-09-11 02:01:23

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

在 Cucumber 中添加“然后向我显示页面”步骤(以便 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 调用现在可以正常工作。

With a new Rails 3 app, I also had this problem testing a simple method which included a redirect_to call in the controller. The method itself worked fine, but Webrat would return the "You are being redirected." response.

Adding in a 'Then show me the page' step in cucumber (so the page that webrat sees opens in the browser) showed the 'You are being redirected." response with a link to an example.org link.

Based on this I discovered Yannimac's patch ( 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 

Making these changes fixed the issue, so redirect_to calls with Webrat now work correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文