Intermediate TDD in Rails : Eliminating Duplication in Feature Tests


Learn how to reuse methods for feature tests.


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'

Step 2

Create spec/support/features.rb file.

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

Step 3

Replace the following lines:

  click_link 'Register'
  fill_in 'Email', with: ''
  fill_in 'Password', with: '12345678'    
  click_button 'Sign up'


 sign_up('', '12345678')

in spec/features/subscription_spec.rb.

Step 4

Run the test:

$rspec spec/features/subscription_spec.rb 

It passes.


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.


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

Related Articles

Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.