Intermediate TDD in Rails : Eliminating Duplication in Feature Tests

Objective


Learn how to reuse methods for feature tests.

Steps


Step 1

Create spec/support/features folder. Create session_helpers.rb in this folder.

module Features
  module SessionHelpers
    def sign_up(email, password)
      click_link 'Register'
      fill_in 'Email', with: email
      fill_in 'Password', with: password
      click_button 'Sign up'
    end
  end
end

Step 2

Create spec/support/features.rb file.

RSpec.configure do |config|
  config.include Features::SessionHelpers, type: :feature
end

Step 3

Replace the following lines:

  click_link 'Register'
  fill_in 'Email', with: 'bugs2@rubyplus.com'
  fill_in 'Password', with: '12345678'    
  click_button 'Sign up'

with

 sign_up('bugs@rubyplus.com', '12345678')

in spec/features/subscription_spec.rb.

Step 4

Run the test:

$rspec spec/features/subscription_spec.rb 

It passes.

Exercise


Create a subscribe helper in subscription_spec.rb that can subscribe a user to a given plan. Change the subscription_spec.rb to use the helper.

Summary


In this lesson you learned how to create, configure and use helpers for feature tests.


Related Articles