Starkers
Starkers

Reputation: 10551

Rails integration tests. Testing for actual pages

When using automated tests to test that my custom routes work, all I do is this:

test "that /logout route opens the logout page" do
    get '/logout'
    assert_response :success
end

or test "that /logout route opens the logout page" do get '/logout' assert_response :redirect end Is this a good enough test? To my mind it seems a bit vague. How could I write a test to explicitly verify the /logout route is actually going to the logout page/the user is served the logout view?

Upvotes: 1

Views: 367

Answers (2)

fontno
fontno

Reputation: 6852

I would suggest that you look into rspec/rails and capybara for integration and or acceptance tests. These tests are more descriptive and sounds like what you may be asking about. For example, this spec is describing the authentication process

describe "signin" do
before { visit signin_path }

describe "with invalid information" do
  before { click_button "Sign in" }

  it { should have_selector('title', text: 'Sign in') }
  it { should have_selector('div.alert.alert-error', text: 'Invalid') }

  describe "after visiting another page" do
    before { click_link "Home" }
    it { should_not have_selector('div.alert.alert-error') }
  end
end

describe "with valid information" do
  let(:user) { FactoryGirl.create(:user) }
  before do
    fill_in "Email",    with: user.email.upcase
    fill_in "Password", with: user.password
    click_button "Sign in"
  end

  it { should have_selector('title', text: user.name) }

  it { should have_link('Users',    href: users_path) }
  it { should have_link('Profile',  href: user_path(user)) }
  it { should have_link('Settings', href: edit_user_path(user)) }
  it { should have_link('Sign out', href: signout_path) }
  it { should_not have_link('Sign in', href: signin_path) }

  describe "followed by signout" do
    before { click_link "Sign out" }
    it { should have_link('Sign in') }
  end
end

This example was taken from the Ruby on Rails Tutorial book and is free online. It has hundreds of examples on integration testing.

Upvotes: 1

Puhlze
Puhlze

Reputation: 2614

Rails has testing facilities for routing. See http://guides.rubyonrails.org/testing.html#testing-routes for more. This will test that certain requests route to a certain controller and action. If you want to verify that specific pages or page content has been loaded after a request you can use integration / view tests to verify that page specific data is available (like a page title specific to the page in question).

Upvotes: 0

Related Questions