3
 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

In the code above, first assert is passing whereas the second is failing. It should happen otherwise. After click_link_or_button('Login') is executed the page logins I am assuming. What am I doing wrong?

Thanks!

Ava
  • 5,783
  • 27
  • 58
  • 86

3 Answers3

6

For anyone searching this issue nowadays -- with Capybara 2.5 you can do

assert_current_path('/posts/index')

and it will use Capybara's waiting behavior to check if the path changes to what is expected

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
1

What are you actually trying to test? It is not possible for current_path to be both "/logins/new" and "/posts/index" at the same time.

I would say move the first assert to just after the visit step. Then your test will make sense. If it is still failing, then the login action isn't working.

Jamie Penney
  • 9,424
  • 3
  • 29
  • 37
0

It seems that the current_path variable hasn't yet updated its new path value. So you need to wait for the path. I did it with two things: fixed a bug preveting correct current_path variable update, and rspec-wait gem. So the expectation code is the following:

wait_for{ page }.to have_current_path(posts_path)
Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69