I'm really new on testing with rspec and capybara. I'm using devise for authentication on my app and I'll have to use an action to sign in in mostly all of my tests. I currently have a sign in test like this:
before do
@john = User.create!(name: "John", email: "[email protected]", password: "password")
end
scenario "with valid credentials" do
visit "/"
fill_in "Email", with: @john.email
fill_in "Password", with: @john.password
click_button "Log in"
expect(page).to have_content("Signed in successfully")
expect(page).to have_content("Welcome, #{@john.name}")
expect(page).not_to have_link("Sign up")
expect(page).to have_link("Sign out")
end
How could I reuse that piece of code, so when I test another actions that requires the user should be signed in, I do all that before with a dryer code. Thanks in advance!