我实际上在Michael Hartl Rails教程的第9章中被困: 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
中的此代码:
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
中定义的:
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
:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper
end
你有解决方案吗?
答案 0 :(得分:1)
您的user_pages_spec.rb是测试规范。它需要测试帮助程序中的sign_in方法,而不是我非常肯定的应用程序的帮助程序。我前一段时间做过那个教程,但刚刚回顾过。我认为您需要在spec / support / utilities.rb中添加一些内容,为您的规范提供sign_in方法。
Hartl的github回购的最终答案是:
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 final。
在教程中,他在第9.6节中提到了这一点。
上市9.6。用于签署用户的测试助手。
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
这明显不同于决赛,但是,我想这是你需要开始的地方。