Stability can become an issue as web applications evolve and grow – integration tests provide a great way to perform end-to-end tests that validate the application is performing as expected.
Integration test with RSpec and Capybara
When writing integration tests, try to model the test around an actor (user of the system) and the action they are performing.
# spec/features/visitor_signs_up_spec.rb require 'spec_helper' feature 'Visitor signs up' do scenario 'with valid email and password' do sign_up_with 'email@example.com', 'password' expect(page).to have_content('Sign out') end scenario 'with invalid email' do sign_up_with 'invalid_email', 'password' expect(page).to have_content('Sign in') end scenario 'with blank password' do sign_up_with 'firstname.lastname@example.org', '' expect(page).to have_content('Sign in') end def sign_up_with(email, password) visit sign_up_path fill_in 'Email', with: email fill_in 'Password', with: password click_button 'Sign up' end end
Extracting common test functionality
To share code between features move common capybara steps into a Ruby module in the rspec support directory.
# spec/support/features/session_helpers.rb module Features module SessionHelpers def sign_up_with(email, password) visit sign_up_path fill_in 'Email', with: email fill_in 'Password', with: password click_button 'Sign up' end def sign_in user = create(:user) visit sign_in_path fill_in 'Email', with: user.email fill_in 'Password', with: user.password click_button 'Sign in' end end end
Modules must be explicitly included to share the common code between integration tests.
# spec/support/features.rb RSpec.configure do |config| config.include Features::SessionHelpers, type: :feature end
Running the integration tests
$ rspec -fd Visitor signs up with valid email and password with invalid email with blank password Finished in 0.35837 seconds 3 examples, 0 failures
- Maintain application stability with end-to-end test coverage
- Use RSpec to run your integration tests
- Modules allow shared Capybara steps between specs
If you found this useful, you might also enjoy: