简体   繁体   中英

inconsistent Rspec/Capybara tests

I have commented out [get "pages/home"] in my routes.rb file, restarted my entire test environment and the following test STILL passes:

it "should contain 'This is a test'" do
  get 'home'
  response.body.should have_selector("p")
end

From my understanding, capybara runs the rspec tests from a 'browser' in memory. If this is the case the rails routes MUST exist! After commenting out my routes, it is still able to load the pages - I know this because if I remove the <p> selector the test fails. After modifying the routes.rb file I have restarted my testing suite but it doesn't make a difference.

This makes me lose trust in the testing process because I have tests passing that shouldn't pass!!

It looks like this fragment is in a controller spec, which hits your controller directly. In the latest version of rspec, 2.9.0, a bad route won't make this example fail. Routes are outside the purview of a controller spec.

It seems like you intended to write a request spec, which should use capybara's visit method:

# spec/requests/something_spec.rb
require 'spec_helper'

describe 'home page' do

  it "should contain 'This is a test'" do
    visit '/pages/home'
    page.should have_content 'This is a test'
  end

end

See the capybara docs for more examples. Rspec also supports routing specs , but I usually only use those for unusual routing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM