RailsApps / rails3-devise-rspec-cucumber

An example Rails 3.2 app with Devise and RSpec and Cucumber.
http://railsapps.github.io/
445 stars 144 forks source link

Rspec controller tests fail when added authenticate_user to the controller #21

Closed slarti42uk closed 12 years ago

slarti42uk commented 12 years ago

Thanks for the great application templates and tutorials!

I'm not sure that this is an issue with the template app or a wider issue but here goes. I'm using the template as a base for a prototype app that requires login for all parts of it. I've used the rails scaffolds to quickly add some functionality and this has created the rspec controller tests. These all run fine until I add before_filter :authenticate_user! to the controller and then all rspec controller tests fail.

I've followed the steps from the Devise wiki and other stackoverflow posts about adding in the Devise test helpers into spec_helper.rb

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

and a before block into the controller test

before (:each) do
  @user = User.create!({
    :name => 'Test User',
    :email => 'user@test.com',
    :password => 'please',
    :password_confirmation => 'please' 
    })
  sign_in @user
end

I understand that cucumber should be used for the integration testing but I just want to find out if this is something that CAN work. I figure that the generated tests are there for free so every extra bit of test coverage is good, yes?

Thanks in advance.

jcohenho commented 12 years ago

your :email attribute is spelled wrong, try that first.

slarti42uk commented 12 years ago

Cheers. I think that must have been a typo on here. I have checked the code and it's got the correct :email Must have hit a back space when formatting. Ooops

slarti42uk commented 12 years ago

Ok so I found this answer over on stackoverflow: http://stackoverflow.com/questions/9137814/controller-spec-not-logging-in-my-factory-created-user

Seems that in the generated specs with the scaffolds there is a valid_session that conflicts with the factory user. The suggestion in the post is to remove it, which does work, but maybe there is a way to work with it.

Anyway this isn't an issue for here so I'll close it. Cheers