I cant find solution for my problem: cant login user in rspec test.
I tried this code, following by this link - https://github.com/plataformatec/devise/wiki/How-To:-Stub-authentication-in-controller-specs
require 'spec_helper'
require 'factory_girl_rails'
describe "ololo" do
it "blocks unauthenticated access" do
user = User.create(email: "[email protected]", password: "123456")
request.env['warden'].stub(:authenticate!).
and_throw(:warden, {:scope => :user})
visit "/tasks"
page.should have_content("Signed in successfully.")
end
end
/error/
Failure/Error: page.should have_content("Signed in successfully.")
expected there to be text "Signed in successfully." in "Task App Home Login You need to sign in or sign up before continuing. Sign in Email Password Remember me Sign upForgot
your password? Task App by Denys Medynskyi"
also tried this link - http://codingdaily.wordpress.com/2011/09/13/rails-devise-and-rspec/.
Devise wiki tutorial is also not working for me.
Please, I'm stuck hardly. Help me anyone.
Without seeing your error message, I guest one possible reason is you created hard record of User to test db. You may have run this test once, so in second time this creation fails because record with same email exists.
You've required FactoryGirl. Why don't you use FactoryGirl to create the fixture data?
In spec/factories/user.rb
FactoryGirl.define do
factory :user do
email: "[email protected]"
password "123"
end
end
In your test, write
FactoryGirl.create(:user)
Comparing with the tut you lack two steps:
sign_in
step before visit the private page.Maybe you would be good with #2 only if you cleared the db after tests. Anyway factory data is recommended over hard data.