Basic TDD in Rails : Installation and Configuration to Run Feature Specs

Objective


To install and configure our project so that we can write a simple feature spec for the home page.

Discussion


We will use Capybara gem to write feature specs.

Steps


Step 1

Add

gem 'capybara' 

to development, test group in Gemfile. Run bundle:

bundle 

Step 2

Create a features folder under spec.

Step 3

Create a new file call home_page_spec.rb in the spec/features folder

Step 4

Add the following code to home_page_spec.rb.

require "rails_helper"

feature "Home Page", :type => :feature do
  scenario "Home Page must load" do
    visit "/"

    expect(page).to have_text("My Blog")
  end
end

Step 5

Run the feature spec.

$ rspec spec/features/home_page_spec.rb 

You will get ActionController::RoutingError: No route matches GET /

Step 6

Uncomment the line:

root 'welcome#index'

in routes.rb.

Step 7

Run the feature spec again. You will get:

  Failure/Error: visit "/"
     ActionController::RoutingError:
       uninitialized constant WelcomeController

Step 8

Create the welcome controller with index action.

Step 9

Run the feature spec again.

Failure/Error: expect(page).to have_text("My Blog")
       expected to find text "My Blog" in "Welcome#index Find me in app/views/welcome/index.html.erb"

Step 10

Edit the welcome/index.html.erb and add the 'My Blog' text.

<h1>My Blog</h1>

Step 11

Run the feature spec again.

1 example, 0 failures

It passes. Let's cleanup.

Step 12

Open the routes.rb and delete the line:

get 'welcome/index'

Step 13

Run the spec again.

Step 14

Let's fix the warning:

/Users/zepho/projects/tdd/basics/blog/db/schema.rb doesn't exist yet. Run `rake db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter /Users/zepho/projects/tdd/basics/blog/config/application.rb to limit the frameworks that will be loaded.

by running the rake db:migrate

Step 15

Run the feature spec. It will pass.

Step 16

In rails_helper.rb, add:

Capybara.default_driver = :selenium

to the end of the file.

Step 17

Run the feature spec.

Capybara's selenium driver is unable to load `selenium-webdriver`, please install the gem and add `gem 'selenium-webdriver'` to your Gemfile if you are using bundler.

Step 18

Add the selenium-webdriver gem to Gemfile and run bundle.

Step 19

Run the feature spec. The test will run the feature spec in the Firefox browser. By default, Capybara uses the :rack_test driver, we switched to :selenium driver to run our feature specs in the firefox browser.

Step 20

Change the visit '/' to visit root_path and run the test.

Summary


In this lesson, we installed and configured our project to run feature specs. We wrote a simple feature spec and made it pass.


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.