Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 当在上下文中的第二个方法中使用时,designe:subject.current_user的RSpec为nil_Ruby On Rails_Ruby_Rspec_Devise - Fatal编程技术网

Ruby on rails 当在上下文中的第二个方法中使用时,designe:subject.current_user的RSpec为nil

Ruby on rails 当在上下文中的第二个方法中使用时,designe:subject.current_user的RSpec为nil,ruby-on-rails,ruby,rspec,devise,Ruby On Rails,Ruby,Rspec,Devise,我目前正在使用RSpec测试我的Rails 4应用程序,在测试时,我发现了一个奇怪的问题:subject.current_user在上下文中的第二个方法中为nil。代码段: describe 'GET #register_as_team:' do context 'user logged in but not admin:' do login_user it 'should redirect_to user_path if user is not studen

我目前正在使用RSpec测试我的Rails 4应用程序,在测试时,我发现了一个奇怪的问题:subject.current_user在上下文中的第二个方法中为nil。代码段:

  describe 'GET #register_as_team:' do
    context 'user logged in but not admin:' do
      login_user
      it 'should redirect_to user_path if user is not student' do
        get :register_as_team, id: subject.current_user.id
        expect(response).to redirect_to(user_path(subject.current_user))
        expect(flash[:danger]).not_to be_nil
      end

      it 'should redirect_to student_path if user is a non-pending student' do
        student = FactoryGirl.create(:student, user: subject.current_user, is_pending: false)
        get :register_as_team, id: subject.current_user.id
        expect(response).to redirect_to(student_path(student))
      end
    end
  end
因此,当第一次使用
subject.current_user
时,它是正常的,我可以只获取已登录的用户,但在第二种方法中,它返回nil

对于背景信息,
login\u user
如下所示:

module ControllerMacros
  def login_user(user = nil)
    before(:each) do
      # @request.env["devise.mapping"] = Devise.mappings[:user]
      user ||= User.find_by(email: 'default_user@controller.spec')
      user ||= FactoryGirl.create(:user, email: 'default_user@controller.spec', uid: 'default_user.controller.spec')
      sign_in user
    end
  end
end

在一个示例中,
subject
只能是。
当您这样做时,
get:register\u as\u team,id:subject.current\u user.id
,您基本上已经解析了
subject
subject。下一行中未解析当前用户

试试这个:

  describe 'GET #register_as_team:' do
    context 'user logged in but not admin:' do
      login_user
      it 'should redirect_to user_path if user is not student' do
        user = subject.current_user
        get :register_as_team, id: user.id
        expect(response).to redirect_to(user_path(user))
        expect(flash[:danger]).not_to be_nil
      end

  it 'should redirect_to student_path if user is a non-pending student' do
    student = FactoryGirl.create(:student, user: subject.current_user, is_pending: false)
    user = subject.current_user
    get :register_as_team, id: user.id
    expect(response).to redirect_to(student_path(student))
  end
end