3

嘿,我click_link在 rspec rails 中做,然后检查链接页面的内容。内容肯定在那个页面上,但它仍然给我一个错误,它不是。

错误

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"

路线.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
4

2 回答 2

2

看起来您的测试代码在控制器规范而不是集成规范中。您在规范文件中拥有的是集成测试,而不是控制器测试。页面和访问是 capybara dsl 的一部分,在控制器规格中不可用。对于旧版本的 rspec-rails 和 capybara,它应该位于 spec/requests 目录中,或者如果您使用 rspec-rails ~> 2.11.1 & capybara ~>2.0.0beta,它应该位于 spec/features 目录中。

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

更新:

由于您使用的是旧版本的水豚,请重命名并将您的规范文件移动到

someapp/spec/requests/password_resets_spec.rb

或者你甚至可以运行生成器,它将把它放在正确的目录和文件名中

rails generate integration_test password_reset

于 2013-05-06T06:53:11.340 回答
0

我假设您的设置 Capybara.default_selector 设置为 :css。试试这个代码:

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

这具有相同的效果,因为它使用 Capybara 来查找您的元素。如果未找到选择器,Find() 将引发异常

阅读更多:http ://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Finders

于 2013-05-06T03:25:20.473 回答