我在 spec / helpers / session.rb 找到了这个简单的助手:
def sign_in user
session[:remember_token] = user.remember_token
end
但是,当我尝试在规范中使用它时出现错误。以下内容:
context 'when user is is signed in' do
before do
sign_in user
request
end
specify{ expect(flash[:success]).to eq "Signed out successfully" }
end
给了我:
Failure/Error: sign_in user
NameError:
undefined local variable or method `session' for #<RSpec::ExampleGroups::AdminArea::Authentication::GuestVisitsRoot:0x00000004f835f0>
那么如何从规范内部操纵会话呢?有可能吗?
答案 0 :(得分:6)
我相信你在集成测试中看到了这个错误。集成测试不是为检查session
相关内容而设计的。
你可以用这种方式实现同样的目标
def sign_in user
# you can also use
# something like this
# post signin_path, :login => user.login, :password => 'password'
# but i dont recommend this, since this is not how user do it.
visit sign_in_path
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
click_button 'Sign in'
end
然后在测试中,类似
expect(page).to have_content('My Account')
有很多很棒的答案。看看,如果你有任何问题......包括它有问题我将尝试回答