Michael Hartl Rails 教程 - 未定义的方法“sign_in" [英] Michael Hartl Rails Tutorial - undefined method 'sign_in'
问题描述
我实际上被困在 Michael Hartl Rails 教程的第 9 章:http://ruby.railstutorial.org/chapters/更新显示和删除用户#sec-unsuccessful_edits
I'm actually stuck on Chapter 9 in Michael Hartl Rails Tutorial : http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users#sec-unsuccessful_edits
当我运行该命令行时:
$ bundle exec rspec spec/requests/user_pages_spec.rb -e "edit page"
我有那个错误:
Failure/Error: sign_in user
NoMethodError: undefined method 'sign_in' for #<RSpec::Core::ExampleGroupe::Nested_1::Nested_4::Nested_1:0x4e7a0b8>
问题来自 spec/requests/user_pages_spec.rb
中的代码:
Issue come from this code in spec/requests/user_pages_spec.rb
:
describe "edit" do
let(:user) { FactoryGirl.create(:user) }
before do
sign_in user
visit edit_user_path(user)
end
end
但是sign_in
实际上是在app/helpers/sessions_helper.rb
中定义的:
But sign_in
is actually defined in app/helpers/sessions_helper.rb
:
def sign_in(user)
remember_token = User.new_remember_token
cookies.permanent[:remember_token] = remember_token
user.update_attribute(:remember_token, User.encrypt(remember_token))
self.current_user = user
end
def signed_in?
!current_user.nil?
end
def current_user=(user)
@current_user = user
end
def current_user
remember_token = User.encrypt(cookies[:remember_token])
@current_user ||= User.find_by(remember_token: remember_token)
end
并且 SessionsHelper
包含在 app/controllers/application_controller.rb
中:
And SessionsHelper
is include in app/controllers/application_controller.rb
:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper
end
你有解决方案吗?
推荐答案
您的 user_pages_spec.rb 是一个测试规范.它将需要测试助手中的 sign_in 方法,而不是我很确定的应用程序助手.不久前我做了那个教程,但刚刚回顾.我认为您需要在 spec/support/utilities.rb 中添加一些内容,以便为您的规范提供 sign_in 方法.
Your user_pages_spec.rb is a test specification. It will need the sign_in method in a test helper, not the helper for the application I'm pretty sure. I did that tutorial a while back, but just reviewed. I think you need to add something to spec/support/utilities.rb that gives it a sign_in method for your specs.
Hartl 的 github repo 上的最终答案是:
The final answer on Hartl's github repo is:
include ApplicationHelper
def sign_in(user)
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
# Sign in when not using Capybara as well.
cookies[:remember_token] = user.remember_token
end
那应该是你的 spec/support/utilities.rb 最终版.
That should be your spec/support/utilities.rb final.
在教程中,他在第 9.6 节中提到了这一点.
In the tutorial itself, he brings that up in section 9.6.
清单 9.6.用于登录用户的测试助手.
Listing 9.6. A test helper to sign users in.
spec/support/utilities.rb
.
.
.
def sign_in(user, options={})
if options[:no_capybara]
# Sign in when not using Capybara.
remember_token = User.new_remember_token
cookies[:remember_token] = remember_token
user.update_attribute(:remember_token, User.encrypt(remember_token))
else
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
end
这显然与决赛不同,但是,我想这是您需要开始的地方.
That obviously differs from the final, but, I imagine it is where you need to start.
这篇关于Michael Hartl Rails 教程 - 未定义的方法“sign_in"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!