1

I have quite some issues with testing the features of my rails application using RSpec. I have a company controller, and are using devise to make sure that you need to be logged in to access the controller.

I then have integration tests like the following:

describe "with valid information" do
            before do
                fill_in "company_address", with: "My special address"
                fill_in "company_name", with: "My User"
                fill_in "company_contact_email", with: "test@test.com"
                fill_in "company_contact_name", with: "Someone Hello"
            end

            it "should create a company" do
                expect { click_button submit }.to change(Company, :count).by(1)
            end     
        end

It, of course, fails because I don't have a user who is logged in. I have tried creating a test helper like described here, and it works well for controllers, but not for integration tests.

What is the best way to log a user in with Devise, prior to these tests?

Dofs
  • 17,737
  • 28
  • 75
  • 123

1 Answers1

0

add file devise.rb to rspec/support And here is content

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

Another way to solve your problem: Rails integration test with the devise gem

Community
  • 1
  • 1
Tolik Kukul
  • 1,996
  • 16
  • 26
  • I have already done this, but since this is not a controller, this wont work. – Dofs Dec 16 '12 at 19:54
  • 1
    this question looks similar http://stackoverflow.com/questions/4292872/rails-integration-test-with-the-devise-gem – Tolik Kukul Dec 16 '12 at 19:57
  • Thanks, I had looked at it before, but I don't want to test the signup here, just make sure the user is signed in before some of the tests. – Dofs Dec 17 '12 at 18:44
  • Have you tried call devise method `sign_in(:user, @user)` inside your test? – Tolik Kukul Dec 17 '12 at 19:13
  • That didn't work. But I got the question you found to work, even though the test is a bit slow. Thanks a lot. Could you update your answer and I can mark it? – Dofs Dec 17 '12 at 20:19