Week 3, day 2

Work's going slow on re-doing the weekend takeaway challenge, there just aren't enough hours in the day. Besides that we also have new stuff to learn this week. But I do definitely like Cucumber and Sinatra.
In Cucumber you write your actual tests in plain English and then you define the code translation for that sentence structure. That means that you can re-use many instances of a single sentence structure without the need to re-code it.

Feature: Starting the game
  In order to play battleships
  As a nostalgic player
  I want to start a new game

Scenario: Homepage
  Given I am on the homepage
  When I follow "New Game"
  Then I expect to see "What's your name?"

Scenario: New Game
  Given I am on New Game page
  When I fill in "Name" with "Tupac"
  And click on "Submit"
  Then I expect to see "Sup Tupac!"

Scenario: Does not input name
  Given I am on New Game page
  When I fill in "Name" with ""
  And click on "Submit"
  Then I expect to see "Please enter name"

Below is the code that allows the above tests to be translated into code.

 1 Given(/^I am on the homepage$/) do
 2   visit '/'
 3 end
 4 
 5 When(/^I follow "([^"]*)"$/) do |link|
 6   click_link link
 7 end
 8 
 9 Then(/^on homepage I should see "([^"]*)"$/) do |arg1|
10   expect(page).to have_content(arg1)
11 end
12 
13 Given(/^I am on New Game page$/) do
14   visit '/new_game'
15 end
16 
17 When(/^I fill in "([^"]*)" with "([^"]*)"$/) do |arg1, arg2|
18   fill_in(arg1, :with => arg2)
19 end
20 
21 When(/^click on "([^"]*)"$/) do |arg1|
22   click_button arg1
23 end
24 
25 Then(/^I expect to see "([^"]*)"$/) do |arg|
26   expect(page).to have_content(arg)
27 end
Written on March 31, 2015