断言current_path

时间:2011-10-13 22:40:49

标签: ruby-on-rails integration-testing capybara

 require 'test_helper'

     class MyTest < ActionController::IntegrationTest

      test "view posts from login page" do
      visit("/logins/new")
      find_field('Username').set('abode')
      find_field('Password').set('efghi')
      click_link_or_button('Login')
      #assert current_path == "/logins/new"
      assert current_path == "/posts/index" 
      end

    end

在上面的代码中,第一个断言正在传递,而第二个断言正在失败。否则应该发生。执行click_link_or_button('Login')后,我假设的页面登录。我做错了什么?

谢谢!

3 个答案:

答案 0 :(得分:3)

对于现在搜索此问题的任何人 - 使用Capybara 2.5,你可以做到

assert_current_path('/posts/index')

它将使用Capybara的等待行为来检查路径是否变为预期的

答案 1 :(得分:1)

你究竟想测试什么? current_path不可能同时是“/ logins / new”和“/ posts / index”。

我想说在访问步骤之后移动第一个断言。那你的测试就有意义了。如果仍然失败,则登录操作无效。

答案 2 :(得分:1)

似乎current_path变量尚未更新其新路径值。所以你需要等待路径。我做了两件事:fixed a bug preveting correct current_path variable updaterspec-wait gem。期望代码如下:

wait_for{ page }.to have_current_path(posts_path)