设计,未定义的局部变量或方法current_user

时间:2012-07-27 18:28:57

标签: ruby-on-rails-3 devise

我有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_insign_out似乎工作正常,但我在current_user上遇到错误:

undefined local variable or method `current_user'

可能有什么问题,如何解决?

1 个答案:

答案 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是用户......

在每个规范之后,您无需退出使用。