Intermediate TDD in Rails : Register for an Account


To implement signup for an account feature.

We now have devise gem installed and configured. When a user registers and logs in to our site, we will present the different subscription plans they can choose from. We can now write tests and drive the implementation in this lesson.

High Level View

Here are the steps for signup feature:

  1. A user clicks on Register link on the home page
  2. Fill out email and password
  3. The user record must be created in our database
  4. The user must be automatically signed in after successful registration

We will not send any confirmation emails because we want the user to experience our great SaaS app immediately.


Step 1

Let's translate the steps we have in the high level view to a feature test. Create a spec/features/user_registration_spec.rb.

require 'rails_helper'
require 'spec_helper'

feature "User Registration", :type => :feature do
  scenario "success" do
    visit root_path
    click_link 'Register'
    fill_in 'Email', with: ''
    fill_in 'Password', with: 'secret'

    expect(page).to have_text("You are logged in")


Step 2

Run the test. rspec spec/features/user_registration_spec.rb.

Unable to find link "Register"

Step 3


 <%= link_to 'Register', new_user_registration_path %>

to the app/views/layouts/application.html.erb

Step 4

Run the test.

Step 5

The test fails.

We want a simple registration page with minimal work for the user to register. Let's get rid of the password confirmation field in the login page. Delete :

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %>

in app/views/devise/registrations/new.html.erb

Step 6

Run the test again.

Step 7

We forgot to click the Signup button. Let's revise our test:

  scenario "success" do
    visit root_path
    click_link 'Register'
    fill_in 'Email', with: ''
    fill_in 'Password', with: 'secret'

    click_button 'Sign up'

    expect(page).to have_text("You are logged in")

Run the test.

Step 8

We get the error:

expected to find text "You are logged in" in "Register Sign up 1 error prohibited this user from being saved: Password is too short (minimum is 8 characters) Email Password (8 characters minimum) Log in"

Change the password value:

fill_in 'Password', with: '12345678'

Run the test.

Step 9

It fails:

1) User Registration success
   Failure/Error: expect(page).to have_text("You are logged in")
expected to find text "You are logged in" in "Register Welcome! You have signed up successfully. My Blog My Blog"

For now, the default message by devise is good enough. Let's change the expectation as follows:

expect(page).to have_text("You have signed up successfully.")

Step 10

Run the test. The test passes.


In this lesson we implemented the user registration feature driving by test.

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.