我正在阅读 Michael Hartl 的教程。
我刚刚介绍了这些测试user_pages_spec.rb
describe "edit" do
let(:user) { FactoryGirl.create(:user) }
before do
sign_in user
visit edit_user_path(user)
end
describe "page" do
it { should have_content("Update your profile") }
it { should have_title("Edit user") }
it { should have_link('change', href: 'http://gravatar.com/emails') }
end
describe "with invalid information" do
before { click_button "Save changes" }
it { should have_content('error') }
end
end
我的sessions_helper.rb
文件是:
module SessionsHelper
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 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
def signed_in?
!current_user.nil?
end
def sign_out
self.current_user = nil
cookies.delete(:remember_token)
end
end
当我运行测试时,所有 4 个都失败,错误消息如下:
1) User pages edit page
Failure/Error: sign_in user
NoMethodError:
undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_4::Nested_1:0x007fcd57928138>
# ./spec/requests/user_pages_spec.rb:90:in `block (3 levels) in <top (required)>'
我应该如何让用户页面规范从 session_helper 中获取 sign_in 功能?users_controller 可以使用;这只是我做错的测试文件。
我尝试明确包含 SessionsHelper 并没有帮助。