Feature Tests

Let’s look at how to add a feature test for the home page of our
rails-bootstrap application. This is our first integration test.

Start by creating a folder named “features” within the spec directory:

$ mkdir spec/features

You must name the folder “features” so RSpec will automatically include
the correct test support functions. RSpec applies magic that depends on
the name of the folder.

If your application is simple, put all your feature specs in the
“features” folder. For a complex application, add another level of
organization. RSpec will apply the correct magic to any subfolders.

It is common to organize features by user persona, for example:

    spec/features/admin
    spec/features/user
    spec/features/visitor

In my user stories, I like to distinguish between a “visitor” (sometimes
called a “guest”), who is not logged in and can only access public web
pages, and a “user,” who is logged in. If you implement access control,
you also might have an “admin” user. Complex applications may have
additional personas. Features can also be grouped in subfolders.

Create a folder named “visitor”:

$ mkdir spec/features/visitor

When I’m writing a feature spec, I like to start with a user story and
scenario description in the format recommended for BDD. RSpec doesn’t
require this, so I add the user story and scenario description as
comments.

Create a file for the first feature spec,
spec/features/visitor/home_page_spec.rb:

 Feature: Home Page
   As a visitor
   I want to visit a home page
   So I can learn more about the website

 Scenario: Visit the Home Page
   Given I am a visitor
   When I visit the home page
   Then I should see "Welcome"