我的 ApplicationController 中有以下代码:
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
end
应用程序/helpers/sessions_helper.rb
module SessionsHelper
def sign_in(user)
cookies.permanent[:remember_token]= user.remember_token
self.current_user = user
end
end
我正在使用 rspec-rails 并在我的实用程序.rb 文件中使用了辅助方法“sign_in”:spec/support/utilities.rb
def sign_in(user)
visit signin_path
fill_in "Email",with:user.email
fill_in "Password",with:user.password
click_button 'Sign in'
cookies[:remember_token]=user.remember_token
end
我的 static_pages_spec 看起来像这样:
describe "When signed in" do
let(:user) {FactoryGirl.create(:user)}
before do
sign_in user
end
describe "When no lists are present" do
it "should show message asking to create list" do
page.should have_selector("div",text:"You have no lists ! Create a list now !")
end
end
end
当我运行测试时,它给了我这个错误:
StaticPages 主页 登录时 没有列表时应显示要求创建列表的消息 失败/错误:登录用户 无方法错误: 未定义的方法sign_in'visit' for #<SessionsController:0x00000003630c10> # ./spec/support/utilities.rb:2:in
# ./app/controllers/sessions_controller.rb:13:in click_button'create' # (eval):2:in
# ./spec/support/utilities.rb:5:in block (4 levels) in top (required)>'sign_in' # ./spec/requests/static_pages_spec.rb:24:in
这是 SessionsController 的“创建”动作:
def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
sign_in user # CODE ERRORS OUT HERE..sign_in of spec/support/utilities.rb executed
redirect_back_or
else
flash.now[:error]= "Invalid email/password"
render 'new' ,layout: "signin_fail"
end
end
TL,DR:所以,我的 SessionsController 执行了错误的“sign_in”方法(属于规范/支持而不是 app/helpers.session_helper.rb 的方法)。当我将规范/支持中的助手名称更改为 "log_in" 时,问题就会得到解决。这里可能是什么问题?