This entry is part 2 of 4 in the series I18n for Your Rails App

Creating a Location.

In the first part, we built an application that lists locations in English and Spanish.

In this part we will create a new location, showing localization concerns along the way. Remember, we are using Cucumber and BDD to drive out our application.

Let’s write the scenario on how we would do that.

Scenario: Create a new location
Given I am on new location page
And I fill in "Name" with "Shiny location"
When I press "Create"
Then I should see "Shiny location"

The scenario is pretty short, and does not mention any implementation details. Currently, When we run Cucumber it will run all the scenarios. We really don’t need to do that since we are only focusing on the Create a new location scenario. Luckily, it’s easy to tell Cucumber to focus on just the “work in process” scenarios. Above the new scenario, add the @wip tag. (wip = Work In Progress)
It should look like this now.

@wip
Scenario: Create a new location
Given I am on new location page
And I fill in "Name" with "Shiny location"
When I press "Create"
Then I should see "Shiny location"

Excellent, now we can run Cucumber and just focus on the current scenario.

$ cucumber --profile wip
Using the wip profile...
Feature: Manage locations
  In order to manage locations
  As a user
  I want to create and edit my locations.

  @wip
  Scenario: Create a new location # features/manage_locations.feature:19
    Given I am on new location page # features/manage_locations.feature:20
    And I fill in "Name" with "Shiny location" # features/manage_locations.feature:21
    When I press "Create" # features/manage_locations.feature:22
    Then I should see "Shiny location" # features/step_definitions/location_steps.rb:9

1 scenario (1 undefined)
4 steps (1 skipped, 3 undefined)
0m0.465s

You can implement step definitions for undefined steps with these snippets:

Given /^I am on new location page$/ do
pending # express the regexp above with the code you wish you had
end

Given /^I fill in "([^"]*)" with "([^"]*)"$/ do |arg1, arg2|
  pending # express the regexp above with the code you wish you had
end

When /^I press "([^"]*)"$/ do |arg1|
  pending # express the regexp above with the code you wish you had
end


The --wip switch was used, so the failures are expected.

FAIL! As expected, Cucumber tells us that we have undefined scenarios. Let’s chip away at this scenario and get it green.
Copy the step definitions for undefined steps into the features/step_definitions/location_step.rb file.

Given /^I am on new location page$/ do
  pending # express the regexp above with the code you wish you had
end

Given /^I fill in "([^"]*)" with "([^"]*)"$/ do |arg1, arg2|
  pending # express the regexp above with the code you wish you had
end

When /^I press "([^"]*)"$/ do |arg1|
  pending # express the regexp above with the code you wish you had
end

Starting with the first step definition, we visit the new_location_path.

Given /^I am on new location page$/ do
  visit(new_locations_path)
end

Running the scenario now yields the following output.

$ cucumber --profile wip
Using the wip profile...
Feature: Manage locations
  In order to manage locations
  As a user
  I want to create and edit my locations.

  @wip
  Scenario: Create a new location # features/manage_locations.feature:19
    Given I am on new location page # features/step_definitions/location_steps.rb:22
      undefined local variable or method `new_location_path' for #<Cucumber::Rails::World:0x0000010332f330> (NameError)
./features/step_definitions/location_steps.rb:23:in `/^I am on new location page$/'
      features/manage_locations.feature:20:in `Given I am on new location page'
    And I fill in "Name" with "Shiny location" # features/step_definitions/location_steps.rb:26
    When I press "Create" # features/step_definitions/location_steps.rb:30
    Then I should see "Shiny location" # features/step_definitions/location_steps.rb:9

Failing Scenarios:
cucumber -p wip features/manage_locations.feature:19 # Scenario: Create a new location

1 scenario (1 failed)
4 steps (1 failed, 3 skipped)
0m0.771s

The --wip switch was used, so the failures were expected.

The “undefined local variable or method `new_location_path’?” tells us that we need to add a route. Open the routes.rb file in the /config folder and add the new route.

match 'locations/new' => 'locations#new', :as => :new_locations

Now what happens when we run the test?

$ cucumber --profile wip
Using the wip profile...
Feature: Manage locations
  In order to manage locations
  As a user
  I want to create and edit my locations.

  @wip
  Scenario: Create a new location # features/manage_locations.feature:19
    Given I am on new location page # features/step_definitions/location_steps.rb:22
      The action 'new' could not be found for LocationsController (AbstractController::ActionNotFound)
      ./features/step_definitions/location_steps.rb:23:in `/^I am on new location page$/'
features/manage_locations.feature:20:in `Given I am on new location page'
    And I fill in "Name" with "Shiny location" # features/step_definitions/location_steps.rb:26
    When I press "Create" # features/step_definitions/location_steps.rb:30
    Then I should see "Shiny location" # features/step_definitions/location_steps.rb:9

Failing Scenarios:
cucumber -p wip features/manage_locations.feature:19 # Scenario: Create a new location

1 scenario (1 failed)
4 steps (1 failed, 3 skipped)
0m0.527s

The --wip switch was used, so the failures were expected.

Of course, the action ‘new’ could not be found for LocationsController. Cucumber is guiding us in creating our Rails app. Let’s add that to the locations_controller.rb file in the /app/controllers folder.

  # GET /locations/new
  # GET /locations/new.json
  def new
    @location = Location.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @location }
    end
  end

OK, Cucumber, tell us what is happening now…

$ cucumber --profile wip
Using the wip profile...
Feature: Manage locations
  In order to manage locations
  As a user
  I want to create and edit my locations.

  @wip
  Scenario: Create a new location # features/manage_locations.feature:19
    Given I am on new location page # features/step_definitions/location_steps.rb:22
      Missing template locations/new, application/new with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
        * "/Users/john/Sites/zzz/app/views"
       (ActionView::MissingTemplate)
      /Users/john/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/benchmark.rb:295:in `realtime'
./app/controllers/locations_controller.rb:16:in `new'
      ./features/step_definitions/location_steps.rb:23:in `/^I am on new location page$/'
features/manage_locations.feature:20:in `Given I am on new location page'
    And I fill in "Name" with "Shiny location" # features/step_definitions/location_steps.rb:26
    When I press "Create" # features/step_definitions/location_steps.rb:30
    Then I should see "Shiny location" # features/step_definitions/location_steps.rb:9

Failing Scenarios:
cucumber -p wip features/manage_locations.feature:19 # Scenario: Create a new location

1 scenario (1 failed)
4 steps (1 failed, 3 skipped)
0m0.546s

The --wip switch was used, so the failures were expected.

Another expected outcome, we need to create our view for the action. In the /app/views/locations folder create a new.html.erb file. In that we will create the form.

<%= form_for(@location) do |f| %>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
  </p>

<p>
<%= f.submit %>
</p>
<% end %>

If I know my Rails and BDD, we should be through the first step.

$ cucumber --profile wip
Using the wip profile...
Feature: Manage locations
  In order to manage locations
  As a user
  I want to create and edit my locations.

  @wip
  Scenario: Create a new location # features/manage_locations.feature:19
    Given I am on new location page # features/step_definitions/location_steps.rb:22
    And I fill in "Name" with "Shiny location" # features/step_definitions/location_steps.rb:26
      TODO (Cucumber::Pending)
      ./features/step_definitions/location_steps.rb:27:in `/^I fill in "([^"]*)" with "([^"]*)"$/'
features/manage_locations.feature:21:in `And I fill in "Name" with "Shiny location"'
    When I press "Create" # features/step_definitions/location_steps.rb:30
    Then I should see "Shiny location" # features/step_definitions/location_steps.rb:9

1 scenario (1 pending)
4 steps (2 skipped, 1 pending, 1 passed)
0m0.662s

The --wip switch was used, so the failures were expected.

Yup, the first step is passing. Time to focus on the second step. The code to satisfy that step sounds just like the step name (thank you, Capybara!)

Given /^I fill in "([^"]*)" with "([^"]*)"$/ do |field, value|
  fill_in(field, :with => value)
end

Can you guess what Cucumber will tell us to do next?

$ cucumber --profile wip
Using the wip profile...
Feature: Manage locations
  In order to manage locations
  As a user
  I want to create and edit my locations.

  @wip
  Scenario: Create a new location # features/manage_locations.feature:19
    Given I am on new location page # features/step_definitions/location_steps.rb:22
    And I fill in "Name" with "Shiny location" # features/step_definitions/location_steps.rb:26
    When I press "Create" # features/step_definitions/location_steps.rb:30
      TODO (Cucumber::Pending)
      ./features/step_definitions/location_steps.rb:31:in `/^press "([^"]*)"$/'
features/manage_locations.feature:22:in `When I press "Create"'
Then I should see "Shiny location" # features/step_definitions/location_steps.rb:9

1 scenario (1 pending)
4 steps (1 skipped, 1 pending, 2 passed)
0m0.561s

The --wip switch was used, so the failures were expected. All is good.

Now we need to click the create button. Again, the Capybara helpers are our friend. In the locations_steps file, make the “When I press” step look like:

When /^I press "([^"]*)"$/ do |button|
  click_button(button)
end

Let’s run the test.

$ cucumber --profile wip
Using the wip profile...
Feature: Manage locations
  In order to manage locations
  As a user
  I want to create and edit my locations.

  @wip
  Scenario: Create a new location # features/manage_locations.feature:19
    Given I am on new location page # features/step_definitions/location_steps.rb:22
    And I fill in "Name" with "Shiny location" # features/step_definitions/location_steps.rb:26
    When I press "Create" # features/step_definitions/location_steps.rb:30
    Then I should see "Shiny location" # features/step_definitions/location_steps.rb:9
      expected there to be content "Shiny location" in "ZzznnLocationsnn" (RSpec::Expectations::ExpectationNotMetError)
      ./features/step_definitions/location_steps.rb:11:in `/^(?:|I )should see "([^"]*)"$/'
features/manage_locations.feature:23:in `Then I should see "Shiny location"'

Failing Scenarios:
cucumber -p wip features/manage_locations.feature:19 # Scenario: Create a new location

1 scenario (1 failed)
4 steps (1 failed, 3 passed)
0m0.612s

The --wip switch was used, so the failures were expected. All is good.

We expected to see the content “Shiny location” in “Internationalization\n\nLocations\n\n”. Let’s look at it closely.

Why is it going to /locations? It should be going to /locations/create. It must be a routing issue, so open the routes.rb file and within the locale scope and add:

match "locations" => 'locations#create', :as => :locations, :via => [:post]

We can verify our routes look correct using rake routes:

$ rake routes
    locations (/:locale)/locations(.:format) locations#index
new_locations (/:locale)/locations/new(.:format) locations#new
    locations POST (/:locale)/locations(.:format) locations#create
         root /(:locale)(.:format) locations#index

That looks right, so let’s run the scenario.

$ cucumber --profile wip
Using the wip profile...
Feature: Manage locations
  In order to manage locations
  As a user
  I want to create and edit my locations.

  @wip
  Scenario: Create a new location # features/manage_locations.feature:19
    Given I am on new location page # features/step_definitions/location_steps.rb:22
    And I fill in "Name" with "Shiny location" # features/step_definitions/location_steps.rb:26
    When I press "Create" # features/step_definitions/location_steps.rb:30
    Then I should see "Shiny location" # features/step_definitions/location_steps.rb:9
      expected there to be content "Shiny location" in "ZzznnLocationsnn" (RSpec::Expectations::ExpectationNotMetError)
      ./features/step_definitions/location_steps.rb:11:in `/^(?:|I )should see "([^"]*)"$/'
features/manage_locations.feature:23:in `Then I should see "Shiny location"'

Failing Scenarios:
cucumber -p wip features/manage_locations.feature:19 # Scenario: Create a new location

1 scenario (1 failed)
4 steps (1 failed, 3 passed)
0m0.613s

The --wip switch was used, so the failures were expected. All is good.

Really? The same error? Let’s look back at our routes. The first route in the file is the index action. The way routes work is the first match wins. Since both routes have the name of locations, the index action always wins. We have Two possible solutions. Move the create action route to the top or add :via => get to the index action.
I like the latter. Let’s make the index action route look like:

match 'locations/' => 'locations#index', :as => :locations, :via => [:get]

Recheck the routes:

    locations GET (/:locale)/locations(.:format) locations#index
new_locations (/:locale)/locations/new(.:format) locations#new
    locations POST (/:locale)/locations(.:format) locations#create
         root /(:locale)(.:format) locations#index

OK, only GET actions will call the index page. Our scenario should be happy, or at least, not complaining about routes any more.

$ cucumber --profile wip
Using the wip profile...
Feature: Manage locations
  In order to manage locations
  As a user
  I want to create and edit my locations.

  @wip
  Scenario: Create a new location # features/manage_locations.feature:19
    Given I am on new location page # features/step_definitions/location_steps.rb:22
    And I fill in "Name" with "Shiny location" # features/step_definitions/location_steps.rb:26
    When I press "Create" # features/step_definitions/location_steps.rb:30
      The action 'create' could not be found for LocationsController (AbstractController::ActionNotFound)
      (eval):2:in `click_button'
./features/step_definitions/location_steps.rb:31:in `/^I press "([^"]*)"$/'
      features/manage_locations.feature:22:in `When I press "Create"'
    Then I should see "Shiny location" # features/step_definitions/location_steps.rb:9

Failing Scenarios:
cucumber -p wip features/manage_locations.feature:19 # Scenario: Create a new location

1 scenario (1 failed)
4 steps (1 failed, 1 skipped, 2 passed)
0m0.558s

The --wip switch was used, so the failures were expected. All is good.

Sure enough, there is a new error. The action 'create' could not be found for LocationsController. You can see how using Cucumber to guide the design of a Rails application can get into a nice repeatable groove of creating the route, action, and view for a scenario.

Here, we forgot the C part of CRUD. Open the locations_controller and add the create method.

  # POST /locations
  # POST /locations.json
  def create
    @location = Location.new(params[:location])

    respond_to do |format|
      if @location.save
        format.html { redirect_to @location, notice: 'Location was successfully created.' }
        format.json { render json: @location, status: :created, location: @location }
      else
        format.html { render action: "new" }
        format.json { render json: @location.errors, status: :unprocessable_entity }
      end
    end
  end

Let’s run the test.

$ cucumber --profile wip
Using the wip profile...
Feature: Manage locations
  In order to manage locations
  As a user
  I want to create and edit my locations.

  @wip
  Scenario: Create a new location # features/manage_locations.feature:19
    Given I am on new location page # features/step_definitions/location_steps.rb:22
    And I fill in "Name" with "Shiny location" # features/step_definitions/location_steps.rb:26
    When I press "Create" # features/step_definitions/location_steps.rb:30
      undefined method `location_url' for #<LocationsController:0x00000100c24330> (NoMethodError)
./app/controllers/locations_controller.rb:29:in `block (2 levels) in create'
      ./app/controllers/locations_controller.rb:27:in `create'
(eval):2:in `click_button'
      ./features/step_definitions/location_steps.rb:31:in `/^I press "([^"]*)"$/'
features/manage_locations.feature:22:in `When I press "Create"'
Then I should see "Shiny location" # features/step_definitions/location_steps.rb:9

Failing Scenarios:
cucumber -p wip features/manage_locations.feature:19 # Scenario: Create a new location

1 scenario (1 failed)
4 steps (1 failed, 1 skipped, 2 passed)
0m0.652s

The --wip switch was used, so the failures were expected. All is good.

Hmm. In the create method we redirect it to @locations. We need to add a route for that.
In the routes.rb file add:

match "locations/show/(:id)" => 'locations#show', :as => :location

Does that complete the scenario?

$ cucumber --profile wip
Using the wip profile...
Feature: Manage locations
  In order to manage locations
  As a user
  I want to create and edit my locations.

  @wip
  Scenario: Create a new location # features/manage_locations.feature:19
    Given I am on new location page # features/step_definitions/location_steps.rb:22
    And I fill in "Name" with "Shiny location" # features/step_definitions/location_steps.rb:26
    When I press "Create" # features/step_definitions/location_steps.rb:30
      The action 'show' could not be found for LocationsController (AbstractController::ActionNotFound)
      (eval):2:in `click_button'
./features/step_definitions/location_steps.rb:31:in `/^I press "([^"]*)"$/'
      features/manage_locations.feature:22:in `When I press "Create"'
    Then I should see "Shiny location" # features/step_definitions/location_steps.rb:9

Failing Scenarios:
cucumber -p wip features/manage_locations.feature:19 # Scenario: Create a new location

1 scenario (1 failed)
4 steps (1 failed, 1 skipped, 2 passed)
0m0.653s

The --wip switch was used, so the failures were expected. All is good.

Nope, looks like we need to add the show action. Open the locations_controller.rb file and add:

  # GET /locations/1
  # GET /locations/1.json
  def show
    @location = Location.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @location }
    end
  end

I am sure it’ll work now.

$ cucumber --profile wip
Using the wip profile...
Feature: Manage locations
  In order to manage locations
  As a user
  I want to create and edit my locations.

  @wip
  Scenario: Create a new location # features/manage_locations.feature:19
    Given I am on new location page # features/step_definitions/location_steps.rb:22
    And I fill in "Name" with "Shiny location" # features/step_definitions/location_steps.rb:26
    When I press "Create" # features/step_definitions/location_steps.rb:30
      Missing template locations/show, application/show with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
        * "/Users/john/Sites/zzz/app/views"
       (ActionView::MissingTemplate)
      /Users/john/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/benchmark.rb:295:in `realtime'
./app/controllers/locations_controller.rb:43:in `show'
      (eval):2:in `click_button'
./features/step_definitions/location_steps.rb:31:in `/^I press "([^"]*)"$/'
      features/manage_locations.feature:22:in `When I press "Create"'
    Then I should see "Shiny location" # features/step_definitions/location_steps.rb:9

Failing Scenarios:
cucumber -p wip features/manage_locations.feature:19 # Scenario: Create a new location

1 scenario (1 failed)
4 steps (1 failed, 1 skipped, 2 passed)
0m0.613s

The --wip switch was used, so the failures were expected. All is good.

Oops, seems we need to add a show.html.erb file. Create one in the /app/views/locations folder and add the code to display the location.

<p><%= @location.name %></p>

Yeah, it’s not real fancy, but we’re just getting the tests to pass.

$ cucumber --profile wip
Using the wip profile...
Feature: Manage locations
  In order to manage locations
  As a user
  I want to create and edit my locations.

  @wip
  Scenario: Create a new location # features/manage_locations.feature:19
    Given I am on new location page # features/step_definitions/location_steps.rb:22
    And I fill in "Name" with "Shiny location" # features/step_definitions/location_steps.rb:26
    When I press "Create" # features/step_definitions/location_steps.rb:30
    Then I should see "Shiny location" # features/step_definitions/location_steps.rb:9

1 scenario (1 passed)
4 steps (4 passed)
0m0.585s

The --wip switch was used, so I didn't expect anything to pass. These scenarios passed:
(::) passed scenarios (::)

features/manage_locations.feature:19:in `Scenario: Create a new location'

ALL GREEN!
Ready for a break?

La Creación de Nuevas Ubicaciones

The English (default) language scenario is passing, now it’s time to focus on Spanish. Create a new scenario in the /features/manage_locations.feature.

@wip
Scenario: Create a new location
Given I am on the es site
And I am on new location page
And I fill in "Name" with "Shiny location"
When I press "crear"
Then I should see "Shiny location"

I bet you can guess what the output of running this scenario will be…

$ cucumber --profile wip
Using the wip profile...
Feature: Manage locations
  In order to manage locations
  As a user
  I want to create and edit my locations.

  @wip
  Scenario: Create a new location # features/manage_locations.feature:19
    Given I am on new location page # features/step_definitions/location_steps.rb:22
    And I fill in "Name" with "Shiny location" # features/step_definitions/location_steps.rb:26
    When I press "Create" # features/step_definitions/location_steps.rb:30
    Then I should see "Shiny location" # features/step_definitions/location_steps.rb:9

  @wip
  Scenario: Create a new location # features/manage_locations.feature:25
    Given I am on the es site # features/step_definitions/location_steps.rb:17
    And I am on new location page # features/step_definitions/location_steps.rb:22
    And I fill in "Nombre" with "buena ubicación" # features/step_definitions/location_steps.rb:26
      cannot fill in, no text field, text area or password field with id, name, or label 'Nombre' found (Capybara::ElementNotFound)
      (eval):2:in `fill_in'
./features/step_definitions/location_steps.rb:27:in `/^I fill in "([^"]*)" with "([^"]*)"$/'
      features/manage_locations.feature:28:in `And I fill in "Nombre" with "buena ubicación"'
When I press "crear" # features/step_definitions/location_steps.rb:30
Then I should see "buena ubicación" # features/step_definitions/location_steps.rb:9

Failing Scenarios:
cucumber -p wip features/manage_locations.feature:25 # Scenario: Create a new location

2 scenarios (1 failed, 1 passed)
9 steps (1 failed, 2 skipped, 6 passed)
0m1.319s

The --wip switch was used, so I didn't expect anything to pass. These scenarios passed:
(::) passed scenarios (::)

features/manage_locations.feature:19:in `Scenario: Create a new location'

The first two steps are the same, so they pass with flying colors. However, our widgets are now named for the locale we are using (es) and Cucumber can’t find the. Remember the I18n.translate call? We’re using that again for the name field. Open the new.html.erb file in the views folder.

<%= form_for(@location) do |f| %>
<p>
<%= f.label :name, t('.name_html') %>
<%= f.text_field :name %>
  </p>

<p>
<%= f.submit %>
</p>
<% end %>

If we run the test now, both @wip scenarios will fail since we call the translate function and we haven’t supplied the translations. Let’s add those now.
In the /config/locales folder and open the en.yml file and add:

en:
    locations:
        index:
            title_html: "Locations"
        new:
            name_html: "Name"

Don’t forget Spanish. Open the es.yml file and add:

es:
    locations:
        index:
            title_html: "Locaciones"
        new:
            name_html: "Nombre"

I bet it can find the name field in both cases now.

$ cucumber --profile wip
Using the wip profile...
Feature: Manage locations
  In order to manage locations
  As a user
  I want to create and edit my locations.

  @wip
  Scenario: Create a new location # features/manage_locations.feature:19
    Given I am on new location page # features/step_definitions/location_steps.rb:22
    And I fill in "Name" with "Shiny location" # features/step_definitions/location_steps.rb:26
    When I press "Create" # features/step_definitions/location_steps.rb:30
    Then I should see "Shiny location" # features/step_definitions/location_steps.rb:9

  @wip
  Scenario: Create a new location # features/manage_locations.feature:25
    Given I am on the es site # features/step_definitions/location_steps.rb:17
    And I am on new location page # features/step_definitions/location_steps.rb:22
    And I fill in "Nombre" with "buena ubicación" # features/step_definitions/location_steps.rb:26
    When I press "crear" # features/step_definitions/location_steps.rb:30
      no button with value or id or text 'crear' found (Capybara::ElementNotFound)
      (eval):2:in `click_button'
./features/step_definitions/location_steps.rb:31:in `/^I press "([^"]*)"$/'
      features/manage_locations.feature:29:in `When I press "crear"'
Then I should see "buena ubicación" # features/step_definitions/location_steps.rb:9

Failing Scenarios:
cucumber -p wip features/manage_locations.feature:25 # Scenario: Create a new location

2 scenarios (1 failed, 1 passed)
9 steps (1 failed, 1 skipped, 7 passed)
0m0.648s

The --wip switch was used, so I didn't expect anything to pass. These scenarios passed:
(::) passed scenarios (::)

features/manage_locations.feature:19:in `Scenario: Create a new location'

Looks like we need to do the same for the create button. In the new.html.erb page we’ll change the submit button.

  <p>
<%= f.button t('.button_html') %>
</p>

Of course, we have to add the button_html to the locales.
en.yml file:

en:
    locations:
        index:
            title_html: "Locations"
        new:
            name_html: "Name"
            button_html: "Create"

es.yml file:

es:
    locations:
        index:
            title_html: "Locaciones"
        new:
            name_html: "Nombre"
            button_html: "crear"

Are we done?

$ cucumber --profile wip
Using the wip profile...
Feature: Manage locations
  In order to manage locations
  As a user
  I want to create and edit my locations.

  @wip
  Scenario: Create a new location # features/manage_locations.feature:19
    Given I am on new location page # features/step_definitions/location_steps.rb:22
    And I fill in "Name" with "Shiny location" # features/step_definitions/location_steps.rb:26
    When I press "Create" # features/step_definitions/location_steps.rb:30
    Then I should see "Shiny location" # features/step_definitions/location_steps.rb:9

  @wip
  Scenario: Create a new location # features/manage_locations.feature:25
    Given I am on the es site # features/step_definitions/location_steps.rb:17
    And I am on new location page # features/step_definitions/location_steps.rb:22
    And I fill in "Nombre" with "buena ubicación" # features/step_definitions/location_steps.rb:26
    When I press "crear" # features/step_definitions/location_steps.rb:30
    Then I should see "buena ubicación" # features/step_definitions/location_steps.rb:9

2 scenarios (2 passed)
9 steps (9 passed)
0m0.689s

The --wip switch was used, so I didn't expect anything to pass. These scenarios passed:
(::) passed scenarios (::)

features/manage_locations.feature:19:in `Scenario: Create a new location'

features/manage_locations.feature:25:in `Scenario: Create a new location'

Yay! However, before we celebrate we have a couple of things to do. Remove the @wip tags from the feature file and rerun the scenarios.

$ cucumber
Using the default profile...
Feature: Manage locations
  In order to manage locations
  As a user
  I want to create and edit my locations.

  Scenario: List a location. # features/manage_locations.feature:6
    Given there is a location named "location 1" # features/step_definitions/location_steps.rb:1
    When I am on the locations page # features/step_definitions/location_steps.rb:5
    Then I should see "Locations" # features/step_definitions/location_steps.rb:9
    And I should see "location 1" # features/step_definitions/location_steps.rb:9

  Scenario: List a location. # features/manage_locations.feature:12
    Given there is a location named "location 1" # features/step_definitions/location_steps.rb:1
    And I am on the es site # features/step_definitions/location_steps.rb:17
    When I am on the locations page # features/step_definitions/location_steps.rb:5
    Then I should see "Locaciones" # features/step_definitions/location_steps.rb:9
    And I should see "location 1" # features/step_definitions/location_steps.rb:9

  Scenario: Create a new location # features/manage_locations.feature:19
    Given I am on new location page # features/step_definitions/location_steps.rb:22
    And I fill in "Name" with "Shiny location" # features/step_definitions/location_steps.rb:26
      cannot fill in, no text field, text area or password field with id, name, or label 'Name' found (Capybara::ElementNotFound)
      (eval):2:in `fill_in'
./features/step_definitions/location_steps.rb:27:in `/^I fill in "([^"]*)" with "([^"]*)"$/'
      features/manage_locations.feature:21:in `And I fill in "Name" with "Shiny location"'
    When I press "Create" # features/step_definitions/location_steps.rb:30
    Then I should see "Shiny location" # features/step_definitions/location_steps.rb:9

  Scenario: Create a new location # features/manage_locations.feature:25
    Given I am on the es site # features/step_definitions/location_steps.rb:17
    And I am on new location page # features/step_definitions/location_steps.rb:22
    And I fill in "Nombre" with "buena ubicación" # features/step_definitions/location_steps.rb:26
    When I press "crear" # features/step_definitions/location_steps.rb:30
    Then I should see "buena ubicación" # features/step_definitions/location_steps.rb:9

Failing Scenarios:
cucumber features/manage_locations.feature:19 # Scenario: Create a new location

4 scenarios (1 failed, 3 passed)
18 steps (1 failed, 2 skipped, 15 passed)
0m0.707s

What?? On the third scenario it thinks the locale is es since that’s what we set in the second scenario. In the English scenarios we need to add the locale to the scenarios.

Feature: Manage locations
In order to manage locations
As a user
I want to create and edit my locations.

Scenario: List a location.
Given there is a location named "location 1"
And I am on the en site
When I am on the locations page
Then I should see "Locations"
And I should see "location 1"

Scenario: List a location.
Given there is a location named "location 1"
And I am on the es site
When I am on the locations page
Then I should see "Locaciones"
And I should see "location 1"

Scenario: Create a new location
Given I am on the en site
And I am on new location page
And I fill in "Name" with "Shiny location"
When I press "Create"
Then I should see "Shiny location"

Scenario: Create a new location
Given I am on the es site
And I am on new location page
And I fill in "Nombre" with "buena ubicación"
When I press "crear"
Then I should see "buena ubicación"

Fingers crossed…

$ cucumber
Using the default profile...
Feature: Manage locations
  In order to manage locations
  As a user
  I want to create and edit my locations.

  Scenario: List a location. # features/manage_locations.feature:6
    Given there is a location named "location 1" # features/step_definitions/location_steps.rb:1
    And I am on the en site # features/step_definitions/location_steps.rb:17
    When I am on the locations page # features/step_definitions/location_steps.rb:5
    Then I should see "Locations" # features/step_definitions/location_steps.rb:9
    And I should see "location 1" # features/step_definitions/location_steps.rb:9

  Scenario: List a location. # features/manage_locations.feature:13
    Given there is a location named "location 1" # features/step_definitions/location_steps.rb:1
    And I am on the es site # features/step_definitions/location_steps.rb:17
    When I am on the locations page # features/step_definitions/location_steps.rb:5
    Then I should see "Locaciones" # features/step_definitions/location_steps.rb:9
    And I should see "location 1" # features/step_definitions/location_steps.rb:9

  Scenario: Create a new location # features/manage_locations.feature:20
    Given I am on the en site # features/step_definitions/location_steps.rb:17
    And I am on new location page # features/step_definitions/location_steps.rb:22
    And I fill in "Name" with "Shiny location" # features/step_definitions/location_steps.rb:26
    When I press "Create" # features/step_definitions/location_steps.rb:30
    Then I should see "Shiny location" # features/step_definitions/location_steps.rb:9

  Scenario: Create a new location # features/manage_locations.feature:27
    Given I am on the es site # features/step_definitions/location_steps.rb:17
    And I am on new location page # features/step_definitions/location_steps.rb:22
    And I fill in "Nombre" with "buena ubicación" # features/step_definitions/location_steps.rb:26
    When I press "crear" # features/step_definitions/location_steps.rb:30
    Then I should see "buena ubicación" # features/step_definitions/location_steps.rb:9

4 scenarios (4 passed)
20 steps (20 passed)
0m0.841s

Excelente! Todos son verdes.

In the next part of this series we will edit the locations.

EXTRA CREDIT:

Scenario Outlines.

You might have noticed that the first and second scenario were essentially identical. The same is true for the third and fourth scenarios. We can group them together with outlines.
We put place holder in the outline and the corresponding values are in the table.

Feature: Manage locations
In order to manage locations
As a user
I want to create and edit my locations.

Scenario Outline: List locations
Given there is a location named "<location>"
And I am on the <language> site
When I am on the locations page
Then I should see "<title>"
And I should see "<result>"

Examples:
| location | language | title | result |
| location 1 | en | Locations | location 1 |
| location 2 | es | Locaciones | location 2 |

Scenario Outline:: Create a new location
Given I am on the <language> site
And I am on new location page
And I fill in "<name>" with "<location>"
When I press "<button>"
Then I should see "<result>"

Examples:
| language | name | location | button | result |
| en | Name | location 1 | Create | location 1 |
| es | Nombre | location 1 | crear | location 1 |

The beauty of this is that if you want to add a test, for example for name validation, we can easily add it to the Examples as I’ve done below:

Examples:
 | language | name | location | button | result |
 | en | Name | location 1 | Create | location 1 |
 | es | Nombre | location 1 | crear | location 1 |
 | en | Name | | Create | Name cannon be blank |

See you next time. Hasta luego!

Series Navigation<< Easy Internationalization for Your Rails App with BDDEasy Internationalization for Your Rails App with BDD, Part III >>