module ControllerMacros
def login_user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
sign_in Factory.create(:confirmed_user)
end
end
end
Which uses this Factory Girl definition:
Factory.define :user do |f|
f.sequence(:email) {|n| "bob_#{n}@abc.com" }
f.password "123456"
# ...
f.after_build { |f|
f.email_confirmation = f.email unless f.email.presence.nil?
f.password_confirmation = f.password unless
f.password.presence.nil?
}
end
Factory.define :confirmed_user, :parent => :user do |f|
f.after_create { |f| f.confirm! }
end
Then I use the login_user method before my tests.
context "with an authenticated user" do
login_user
it "should have a link to each printable coupon" do
PrintableCoupon.all.each do |coupon|
response.should have_selector("ul.promotions li h4",
:content => coupon.name)
end
end
it "should not display a message to log in to view the coupons"
do
response.should_not have_selector("#login-to-view")
end
end
When I check my test.log file after I run that spec, I can see that
the user is created, the email is generated, and the record is updated
with a confirmation timestamp. However, I don't see any activity for
the sign_in method such as updating the user record with the last
login time, and the tests always fail because the pages display as if
the user is still not logged in. Am I missing something here?
Also, is there a way to test the sign_in method via the Rails console
so I can see if it works after I explicitly create a user?
I'm trying to use the sign_in helper method provided by devise but I
can't make it work.
You may use sign_in with capybara as a workaround.
module ControllerMacros
def login_user
before(:each) do
user = Factory.create(:confirmed_user)
visit new_user_session_path
fill_in 'user_email', :with => user.email
fill_in 'user_password', :with => user.password
click_button 'Sign in'
sign_in Factory.create(:confirmed_user)
end
end
end
On 22 nov, 17:36, Chris Bloom <chrisblo...@gmail.com> wrote:
> I'm following the suggestion athttps://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-...
> for creating a macro file in my RSpec support folder:
>
> module ControllerMacros
> def login_user
> before(:each) do
> @request.env["devise.mapping"] = Devise.mappings[:user]
> sign_in Factory.create(:confirmed_user)
> end
> end
> end
>
> Which uses this Factory Girl definition:
>
> Factory.define :user do |f|
> f.sequence(:email) {|n| "bob_#...@abc.com" }