Alain Goldman
Alain Goldman

Reputation: 2908

Rails - Cannot get Rspec click link to work

Hey I am doing click_link in rspec rails then checking the linked page for content. The content is definitely on that page but it's still giving me an error that it is not.

The error

Failure/Error: page.should have_selector('h1', :text => "Reset Password")

This is what my spec is doing

  1. Visits log_in_path
  2. clicks forgotten password
  3. which in effect visits new_password_reset_path
  4. looks for <h1> Reset Password </h1>

password_resets_controller_spec.rb (the spec)

require 'spec_helper'

describe PasswordResetsController do
    it "emails user when requesting password reset" do
        user = FactoryGirl.build(:user)
        visit log_in_path
        click_link "forgotten password?"
        current_path.should eq(new_password_reset_path)
        page.should have_selector('h1', :text => "Reset Password")
    end
end

new.html.haml (log_in_path)

%h1 Log in

= form_tag sessions_path do
  %p
    = label_tag :email
    = text_field_tag :email, params[:email]
  %p
    = label_tag :password
    = password_field_tag :password
  %p.button
    %input{name: "commit", type: "submit", value: "Log in"}
  .field
    = label_tag :remember_me
    = check_box_tag :remember_me, 1, params[:remember_me]
  %p
    = link_to "forgotten password?", new_password_reset_path

new.html.haml (the page it should go to after clicking link)

%h1 Reset Password

= form_tag password_resets_path, :method => :post do
  .field
    = label_tag :email
    = text_field_tag :email, params[:email]
  .actions
    = submit_tag "Reset Password"

routes.rb

SomeApp::Application.routes.draw do
  get "password_resets/new"
  get "sessions/new"
  resources :sessions
  resources :password_resets
  resources :email_activations
  resources :users do
    collection do 
      get :accept_invitation
    end 
  end
  get "static_pages/home"
  get "static_pages/help"
  root to: 'static_pages#home'
  match "sign_up",  to: "users#new"
  match '/help',    to: 'static_pages#help'
  match '/log_in',  to: 'sessions#new'
  match '/log_out', to: 'sessions#destroy'
  end

Upvotes: 3

Views: 5806

Answers (2)

fontno
fontno

Reputation: 6852

It looks like your test code is in a controller spec instead of an integration spec. What you have in the spec file is an integration test, not a controller test. Page and visit are part of the capybara dsl and are not available in controller specs. it should either be in spec/requests directory for older versions of rspec-rails and capybara, or the spec/features directory if you are using rspec-rails ~> 2.11.1 & capybara ~>2.0.0beta.

source: http://rubydoc.info/gems/rspec-rails/file/Capybara.md

Update:

Since you are using an older version of capybara, rename and move your spec file to

someapp/spec/requests/password_resets_spec.rb

or you could even run the generator and it will put it in the right directory and file name

rails generate integration_test password_reset

Upvotes: 2

Benjamin Bouchet
Benjamin Bouchet

Reputation: 13181

I assume you have in your settings Capybara.default_selector set to :css. Try this code:

page.find('h1', :text => "Reset Password")

This has the same effect, as it uses Capybara to find your element. Find() will raise an exception if the selector is not found

Read more: http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Finders

Upvotes: 0

Related Questions