Maintaining users in a database between tests using Capybara?

My problem is that I have to create a new user and login for each individual capybara test.

The following is an example:

require 'spec_helper'

describe "users" do
  describe "user registration" do
    it "should create a new user and log in" do
      # Register a new user to be used during the testing process
      visit signup_path
      fill_in 'Email', with: 'testuser'
      fill_in 'Password', with: 'testpass'
      fill_in 'Password confirmation', with: 'testpass'
      click_button 'Create User'
      current_path.should == root_path
      page.should have_content 'Thank you for signing up!'
    end
  end

  describe "user login" do
    it "should log in" do

      # log in
      visit login_path
      fill_in 'Email', with: 'testuser'
      fill_in 'Password', with: 'testpass'
      click_button 'Log In'
      current_path.should == root_path
      page.should have_content 'Logged in!'
    end
  end
end

The login test fails because the user no longer exists in the database for this test.

This could be fixed simply by putting both in the same test, but I think this is bad practice.

Also, I have another file that is currently logging and logging between each test using before_do, which also seems pretty bad ... you can see this code here .

For the record, this is my first rails app, so maybe I'm trying to get it wrong. I would like to dry it as much as possible.

capybara , ?

+4
1

.

require "spec_helper"

  describe "Users" do
     subject { page }
     describe "User Registration" do
        before { visit signup_path }

        let(:submit) { "Sign up" }

        describe "with invalid information" do
           it "should not create a user" do
             expect { click_button submit }.not_to change(User, :count)
           end
        end

        describe "with valid information" do
           before do
             fill_in "Email",        with: "user@example.com"
             fill_in "Password",     with: "foobar12"
             fill_in "Password confirmation", with: "foobar12"
           end

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

           describe "after registration" do
             before { click_button submit }
             it { should have_content 'Thank you for signing up!' }
           end

           describe "after registration signout and login" do
              let(:user) { User.find_by_email('user@example.com') }
              before do
                click_button submit 
                visit signout_path
                sign_in user     # sign_in is a method which u can define in your spec/support/utilities.rb . Define once and use at multiple places.
              end
              it { should have_content 'Logged In!' }
              it { should have_link('Logout') }
           end
        end
     end    
   end

# spec/support/utilities.rb

def sign_in(user)
  visit sign_path
  fill_in "Email",    with: user.email
  fill_in "Password", with: user.password
  click_button "Log in" 
end

describe it before , .

+1

Source: https://habr.com/ru/post/1526372/


All Articles