2013-05-06 50 views
3

嘿,我在做rspec rails的click_link然後檢查鏈接頁面的內容。內容絕對是在該頁面,但它仍然給我一個錯誤,它不是。Rails - 無法獲得Rspec點擊鏈接工作

錯誤

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

這是我的規格是做

  1. 訪問log_in_path
  2. 點擊忘記密碼
  3. 這實際上訪問new_password_reset_path
  4. 查找<h1>重置密碼</h1>

password_resets_controller_spec.rb(規範)

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(點擊鏈接後應該去的頁面)

%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 

回答

2

它看起來像你的測試代碼是在控制器規範,而不是一個集成規範。你在spec文件中有什麼是集成測試,而不是控制器測試。頁面和訪問是capybara dsl的一部分,並不適用於控制器規格。它應該在spec/requests目錄中,用於老版本的rspec-rails和capybara,或者是spec/features目錄,如果你使用rspec-rails〜> 2.11.1 & capybara〜> 2.0.0beta。

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

更新:

由於您使用的水豚的舊版本,重命名和移動你的規範文件,以

someapp/spec/requests/password_resets_spec.rb

,或者你甚至可以運行發電機並將其放在正確的目錄和文件名中

rails generate integration_test password_reset

+1

代碼對於集成測試看起來很不錯。您使用的是什麼版本的rspec-rails和水豚? – fontno 2013-05-06 06:57:00

+1

我試過了,我得到了。失敗/錯誤:訪問log_in_path NoMethodError: 未定義的方法'訪問'爲# 2013-05-06 22:10:31

+1

rails 3.2.13和capybara 1.1.2 – 2013-05-06 22:14:21

0

我假設你有你的設置Capybara.default_selector設置爲:CSS。試試這個代碼:

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

這與使用水豚找到你的元素有相同的效果。查找()將引發異常,如果選擇沒有找到

瞭解更多:http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Finders

+1

與它說Capybara :: ElementNotFound:無法找到css「h1」 – 2013-05-06 03:28:44

+0

什麼是您的password_resets_controller_spec.rb文件的目錄路徑? – Benj 2013-05-06 03:30:31

+1

someapp/app/controllers/password_resets_controller.rb – 2013-05-06 03:31:19