When creating a new rails app you can skip the unit tests skeleton by passing the -T arg, what more, we can tell it to use mysql as a database backend, like so:
rails new testapp -T -d mysql
Add rspec and cucumber gems to Gemfile
group :development, :test do
gem 'rspec-rails'
gem 'cucumber-rails'
gem 'capybara'
gem 'database_cleaner'
end
Download and install all needed gems:
bundle install
Install RSpec and cucumber
rails generate rspec:install
rails generate cucumber:install --rspec --capybara
Here I specify that I want capybara as the driver for my web tests.
Now, lets specify our first feature! Copy this text snippet and store it in feature/browse_articles.feature
Feature: Browse articles
So that I can browse through the articles
As a visitor
I want to be able to see an article from a full list of articles and read it
Scenario: Browsing a list of articles
Given an article with the title "stickad tröja"
And an article with the title "stickad mössa"
When I am on the articles page
Then I should see "stickad tröja"
And I should see "stickad mössa"
Setup your database like so:
rake db:migrate ; rake db:setup
Now, if we run our test, like so:
bundle exec rake cucumber
…Cucumber will tell us that:
You can implement step definitions for undefined steps with these snippets:
Given /^an article with the title "([^"]*)"$/ do |arg1|
pending # express the regexp above with the code you wish you had
end
Soo.. lets do just that! Implement your first step definition by entering the following text snippet into features/step_definitions/browse_articles_steps.rb
Given /^an article with the title "([^"]*)"$/ do |text|
Article.create! :title => text
end
Create routes for your new article type by adding the following to your config/routes.rb file
resources :articles
If running the cucumber tests now you’ll see it fails cause we do not yet have a Article model. Lets create it!
rails g model article title:string
rake db:migrate
rake db:test:prepare
We will also need a controller for our articles so lets create that too:
rails generate controller articles
Add a simple index method to it:
def index
@articles = Article.all
end
For the test to be able to see the created articles we’ll need a view so lets create app/view/articles/index.html.erb and put this in it:
<ul>
<% @articles.each do |article| %>
<li><%= article.title %></li>
<% end %>
</ul>
We can now run our cucumber scenario:
bundle exec rake cucumber
and we should see something along the lines of:
Feature: Browse articles
So that I can browse through the articles
As a visitor
I want to be able to see an article from a full list of articles and read it
Scenario: Browsing a list of articles # features/browse_articles.feature:7
Given an article with the title "stickad tröja" # features/step_definitions/browse_articles_steps.rb:1
And an article with the title "stickad mössa" # features/step_definitions/browse_articles_steps.rb:1
When I am on the articles page # features/step_definitions/web_steps.rb:44
Then I should see "stickad tröja" # features/step_definitions/web_steps.rb:105
And I should see "stickad mössa" # features/step_definitions/web_steps.rb:105
1 scenario (1 passed)
5 steps (5 passed)
0m1.544s