Rspec, Devise, Factory girl - how to log in to your Factory account through checking the Rspec controller?

I have a message controller and I just installed Devise to quickly and easily add authentication. Before I installed Devise, I created some tests for the "new" action of my message controller. After installing Devise, my tests failed until I added

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

to my spec_helper.rb file.

I also have a string

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

in my mail dispatcher.

Now, when I run my tests, they all pass, with the exception of 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 am using a Factory girl. The error is caused by the before_filter except parameter. I need the user to be able to log into rspec with the Factory user. How can I sign a Factory user from an RSpec controller test? How to identify Factory user?

I tried this in my .rb factories:

 Factory.define :user do |user| user.name "Test User" user.email "user@example.com" 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 greatly appreciated. Thanks.

+8
controller rspec devise factory-bot
source share
1 answer

I highly recommend deleting your tests with the controller and using Cucumber stories instead - RSpec is not as convenient for testing user material as Cucumber.

In Cucumber, you want to enter the user interface to accurately simulate what the user will do. I would like to do the same in the controller specification, although some people may prefer mocking things instead.

Regarding the undefined method password_salt= , did you migrate the test database?

+7
source share

All Articles