如何在调试模式下显示当前用户?

时间:2015-08-20 07:30:02

标签: ruby-on-rails rspec devise capybara

请帮助显示current_user。

我使用rails4 + devise + rspec + capybara。我尝试登录用户。

试验:

it 'check display username on top panel' do
  @request.env["devise.mapping"] = Devise.mappings[:user]
  user = FactoryGirl.create(:user)
  sign_in user
  visit root_path
  binding.pry      
end 

工厂:

FactoryGirl.define do
  factory :user do
    sequence(:name){ |i| "us#{i}" }
    sequence(:email){ |i| "us#{i}@ad.ad" }
    password 'qwerty'
    password_confirmation{ |u| u.password } 
  end
end

rails_helper:

..........
..............
RSpec.configure do |config|
  ...........
  ..........

  config.include Devise::TestHelpers, :type => :controller
end

但我无法显示当前用户。

控制台输出:

[1] pry(#<RSpec::ExampleGroups::AlbumsController>)> puts user.id
1
=> nil
[2] pry(#<RSpec::ExampleGroups::AlbumsController>)> puts user
#<User:0x000000086cee10>
=> nil
[3] pry(#<RSpec::ExampleGroups::AlbumsController>)> puts current_user
NameError: undefined local variable or method `current_user' for #<RSpec::ExampleGroups::AlbumsController:0x0000000863d348>
from /home/kalinin/.rvm/gems/ruby-2.0.0-p598/gems/rspec-expectations-3.3.0/lib/rspec/matchers.rb:966:in `method_missing'
[4] pry(#<RSpec::ExampleGroups::AlbumsController>)> 

2 个答案:

答案 0 :(得分:1)

如果您需要检查登录用户,请尝试在user_signed_in?后调用sign_in user帮助

抱歉我的英文不好

答案 1 :(得分:0)

您可以使用subject.current_user访问当前登录的用户。