8
votes

I have a Posts controller and I have just installed Devise to add some authentication quickly and easily. Before I installed Devise, I had created some tests for the 'new' action of my Posts controller. After installing Devise, my tests were failing until i added the

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

line to my spec_helper.rb file.

I also have the line

before_filter :authenticate_user!, :except => [:show, :index]

in my PostsController.

Now when I run my tests, they all pass except these 2:

it "should be successful" do
     get :new
     response.should be_success
  end

  it "should have the right title" do
     get :new
     response.should have_selector('title', :content => @base_title + " | New post")
  end

I'm using Factory girl as well. The error is cause by the before_filter except parameter. I need to be able to sign in the user in rspec with a factory user. How do I sign the Factory user in from within the Rspec controller test? How do I define the Factory user?

I have tried this in my factories.rb:

Factory.define :user do |user|  
  user.name                  "Test User"
  user.email                 "[email protected]"
  user.password              "password"
  user.password_confirmation "password"
end

But then I get the error:

NoMethodError:
   undefined method `password_salt=' for #<User:0x00000103cac958>

Any help would be much appreciated. Thanks.

1

1 Answers

7
votes

I highly recommend removing your controller tests and using Cucumber stories instead -- RSpec is not as convenient for testing user-facing stuff as Cucumber is.

In Cucumber, you'd want to log in through the UI to mimic exactly what the user will do. I would tend to do the same thing in a controller spec, though some people might prefer mocking things out instead.

As for undefined method password_salt=, did you migrate your test DB?