gpt4 book ai didi

ruby-on-rails - Rails - 无法获取 Rspec 单击链接以工作

转载 作者:数据小太阳 更新时间:2023-10-29 08:04:46 27 4
gpt4 key购买 nike

嘿,我在做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"

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

最佳答案

看起来您的测试代码在 Controller 规范而不是集成规范中。规范文件中的内容是集成测试,而不是 Controller 测试。 Page 和 visit 是 capybara dsl 的一部分,在 Controller 规范中不可用。对于旧版本的 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

更新:

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

someapp/spec/requests/password_resets_spec.rb

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

rails 生成 integration_test password_reset

关于ruby-on-rails - Rails - 无法获取 Rspec 单击链接以工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16391525/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com