0

我有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'

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

4

1 回答 1

0

这是正常的。您应该创建一个用户实例。你可以这样使用 let :

# 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 是 user ...

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

于 2012-07-27T18:33:58.067 回答