keisei_1092 keisei_1092 - 3 months ago 6
Ruby Question

Can RSpec ignore checking port number?

I wrote a simple spec:

visit root_url
click_button 'Sign in'

expect(current_url).to eq user_sign_in_url


and run this spec, it fails because of following:

expected: "http://example.com/users/sign_in"
got: "http://example.com:12345/users/sign_in"


Why in this case RSpec checked port number?

Can RSpec ignore checking this? Any helps would be appreciated.

Answer

First at all you should use current_path method instead of current_url, because the second one returns the whole url. Another thing is that you are asserting strings (expect current_url to equal some string). This is totally okay in most of the cases, but more safe method is to use have_current_path like this:

expect(page).to have_current_path(...)

It will ensure to complete all preceding actions like click_button.