代码之家  ›  专栏  ›  技术社区  ›  deb

webrat和rails:使用assert_contain after click_按钮让我“你被重定向了”

  •  8
  • deb  · 技术社区  · 16 年前

    我正在使用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-u重定向,但是从Webrat的例子来看,单击按钮,然后单击assert-contain应该有效。

    我刚开始使用webrat,那么我是否遗漏了一些明显的东西?为什么我要坚持重定向响应?

    谢谢!

    德伯

    4 回复  |  直到 15 年前
        1
  •  11
  •   justsee    16 年前

    有了一个新的Rails3应用程序,我还测试了一个简单的方法,该方法包括重定向到控制器中的调用。方法本身运行良好,但Webrat将返回“您正在被重定向”响应。

    在cumber中添加一个“then show me the page”步骤(这样webrat看到的页面会在浏览器中打开)显示“You are being directed.”响应,并提供指向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的呼叫现在可以正常工作。

        2
  •  3
  •   gdelfino    15 年前
        3
  •  0
  •   Joshua Partogi    16 年前

    您的应用程序中有任何身份验证吗?我想重定向是因为你还没有被认证。如果我的假设是正确的,请先编写一个设置来登录Webrat。

        4
  •  0
  •   John Hinnegan    15 年前

    这里是要点,您需要做什么来解决这个问题。

    https://gist.github.com/752766