我有Project
控制器和rspec文件。我以这种方式帮助current_user
帮助:
# projects_controller_spec.rb
describe "user signed in" do
before(:each) { sign_in @user }
after(:each) { sign_out @user}
describe "GET index" do
it "assigns all projects as @projects" do
project = FactoryGirl.create(:project, :user => current_user)
get :index, {}, valid_session
assigns(:projects).should eq([project])
end
end
...
end
帮助者sign_in
和sign_out
似乎工作正常,但我在current_user
上遇到错误:
undefined local variable or method `current_user'
可能有什么问题,如何解决?
答案 0 :(得分:0)
这很正常。您应该创建一个User实例。你可以像这样使用:
# projects_controller_spec.rb
describe "user signed in" do
let(:user) {FactoryGirl.create(:user)}
before(:each) { sign_in user }
after(:each) { sign_out user}
describe "GET index" do
it "assigns all projects as @projects" do
project = FactoryGirl.create(:project, :user => user)
get :index, {}, valid_session
assigns(:projects).should eq([project])
end
end
...
end
你知道current_user是用户......
在每个规范之后,您无需退出使用。