Atomfinger
Atomfinger

Reputation: 714

Testing as a logged in user in rails

I wanna test my functionals in my rails application, but the tests require a user/admin to be logged in. I have looked at many websites, but I cannot grasp what I really should be doing to make it work. I'm sure there there is an easy thing to do, but I cannot seem to find it.

I have seen the stubs command used, but I only get that it is an undefined method and I don't really know what it is supposed to do.

Any ideas on how I can run my tests as if it was an user/admin requesting them?

Thanks for any help I might get.

Upvotes: 1

Views: 529

Answers (1)

Ben Taitelbaum
Ben Taitelbaum

Reputation: 7403

acceptance/integration testing:

  1. If you're using cucumber, just login as the user. Something like:

    fill_in 'users[username]', :with => "my username" fill_in 'users[password]', :with => "my password" click_button "Log In"

  2. If you're using rspec+capybara, you can do something like this (assuming you're using Devise for authentication)

    # spec/spec_helper.rb
    include Warden::Test::Helpers
    
    # in spec/acceptance/some_spec.rb
    ...
    login_as FactoryGirl.create(:user)
    

unit testing

Upvotes: 1

Related Questions