Using Cucumber, is there a way to log a user in without the interface?

cucumber, devise, ruby-on-rails, ruby-on-rails-3

Solution

No, there is no way. In the documentation, with regard to the `sign_in @user` and `sign_out @user` helper methods, it says:

These helpers are not going to work for integration tests driven by Capybara or Webrat. They are meant to be used with functional tests only. Instead, fill in the form or explicitly set the user in session

As you said yourself, it is probably cleanest to do it with a `before :each` block. I like to structure it like the following:

context "login necessary" do
  # Before block
  before do
    visit new_user_session_path
    fill_in "Email", with: "test@test.com"
    fill_in "Password", with: "password"
    click_button "Login"
    assert_contain "You logged in successfully."
  end

  # Actual tests that require the user to be logged in
  it "does everything correctly" do
    # ...
  end
end

context "login not necessary" do
  it "does stuff" do
    # code
  end
end

I found this to be quite useful, since if I change authentication rules (i.e. whether or not the user has to be logged in for a specific path) I can just take the whole test and move it into the other description block, without changing any more code.

Problem

The vast majority of my cucumber features require the user to be logged in. However I don't really need to test the login functionality for every single test. I'm currently using Devise for my authentication. I'm looking for a way to sign a user in with devise, without filling out the sign in form. Is there anyway to do this? I would prefer to not have to use the sign in action for every test.

Original source

Related problems