【问题标题】:Minitest: Capybara error when calling #assert_current_path?Minitest:调用#assert_current_path 时出现Capybara 错误?
【发布时间】:2015-10-28 05:15:20
【问题描述】:

我想比较预期路径和当前路径,以证明测试是否在 Minitest 中通过。

我找到了 Capybara::SessionMatchers#assert_current_path 方法。我想这个方法可以满足我的要求。

但是当我写的时候:

page.assert_current_path()

在我的测试中,错误显示:no #assert_current_path method

我看到Capybara::Session 包含Capybara::SessionMatchers。我很困惑为什么我不能调用#assert_current_path 方法。 Capybara 是否具有比较当前路径和预期路径的功能?因为使用页面内容来证明结果对我来说不稳定。

【问题讨论】:

    标签: ruby-on-rails capybara minitest


    【解决方案1】:

    确保您运行的是 Capybara 2.5+ - assert_current_path 匹配器wasn't added until 2.5.0

    page.assert_current_path('/expected/path')
    

    【讨论】:

    • 这是正确的!该方法可以在我更新 gem 版本后调用。非常感谢,汤姆
    【解决方案2】:

    试试这个

    uri = URI.parse(current_url)
    

    如果您需要检查其他参数

    "#{uri.path}?#{uri.query}".should == your_path(:your_params => 'your_value')
    

    【讨论】:

    • 这会立即比较存在异步路径更改问题(由 JS 等触发) - 使用 assert_current_path/have_current_path 可以利用 Capybaras 等待行为
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    相关资源
    最近更新 更多