Using Cucumber, is there a way to register a user without an interface?

The vast majority of my cucumber functions require a user to log in. However, I do not need to check the input functionality for each individual test. I am currently using Devise for my authentication.

I am looking for a way to sign up a user using a program without filling out a form on the form. Is there any way to do this? I would prefer not to use a mark in action for each test.

+4
source share
2 answers

No, there is no way. The documentation for helper methods sign_in @user and sign_out @user says:

These assistants are not going to work on the integration tests conducted by Capybara or Webrat. They are intended for use with functional tests only. Instead, fill out the form or explicitly set the user in a session

As you said yourself, this is perhaps the cleanest way to do this using the before :each block. I like to structure it as follows:

 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 very useful, because if I change the authentication rules (that is, whether the user should be registered for a certain path), I can just take the whole test and transfer it to another description block, without changing the code.

+2
source

Generally, you should always check the interface. But I think this is an acceptable exception.

I am using an application with capybara with rspec, but it should work for you too.

In the assistant, I have this:

 module LoginHelper def login_as(user) super(user, :scope => :user, :run_callbacks => false) end end RSpec.configure do |config| config.include Warden::Test::Helpers, :type => :feature config.include LoginHelper, :type => :feature config.before :each, :type => :feature do Warden.test_mode! end config.after :each, :type => :feature do Warden.test_reset! end end 

Then in the function:

  background do login_as(user) visit root_path end 

See also:
How to drown out Warden / Devise with rspec in Capybara test

0
source

All Articles