I want to use Devise in my App and i need guest user. I try to use this example - https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user
Now, i want write some rspec tests for user to describe the behavior when user becomes a member (this is logging_in action in application controller).
So, my test now is like this:
post :create_guest_user
guest_user = User.find(session[:guest_user_id])
sign_in(guest_user)
guest_user_id = guest_user.id
# Guest user create resume
post = guest_user.posts.build(:title => 'test of guest user')
post.save.should be_true
guest_user.posts.count.should == 1
# Guest user become a member
I don`t know, how to do '#Guest user become a member' section, i want to chek that new user will be creating after registration and all posts will be belong to him. In application controller i have
def logging_in
guest_user.posts.each do |post|
post.user = current_user
post.save
end
end
How i can do this?
You can't sign_in
with guest user. Actually I don't now why, maybe because it's not valid. Anyway to login as guest you need to add this to ControllerMacros
:
module ControllerMacros
# ...
def login_guest(guest = false)
guest ||= FactoryGirl.create(:guest_user)
@request.env['warden'] = @warden
@request.session[:guest_user_id] = guest.id
end
end
And factory:
FactoryGirl.define do
factory :user do
...
end
factory :guest_user, class: 'User' do
sequence(:email) { Faker::Internet.email }
role :guest
to_create { |instance| instance.save(validate: false) }
end
end
Usage:
describe PagesController, type: :controller do
context '#index' do
before do
login_guest
get :index
end
it { expect(response).to have_http_status(:success) }
it { expect(response).to render_template('pages/index') }
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.