Associating a Message with a User

We haven’t defined our user association on the Message model yet. Let’s do that now:

 class​ Message < ActiveRecord::Base
  belongs_to ​:user
 end

Cucumber now outputs this:

 Feature: See Messages
 
  Scenario: See another user's messages
  Given there is a User
  And the User has posted the message "this is my message"
  When I visit the page for the User
  Undefined step: "I visit the page for the User" (Cucumber::Undefined)
  features/see_messages.feature:5
  Then I should see "this is my message"
  Undefined step: "I should see "this is my message"" (Cucumber::Undefined)
  features/see_messages.feature:6
 
 1 scenario (1 undefined)
 4 steps (2 undefined, 2 passed)
 0m0.126s
 
 You can implement step definitions for undefined steps with these snippets:
 
 When(/^I visit the page for the User$/) do
  pending # Write code here that turns the phrase above into concrete actions
 end
 
 Then(/^I should see "([^"]*)"$/) do |arg1|
  pending # Write code here that turns the phrase above into concrete actions
 end

Great! Our two Given steps are passing. Notice how nicely our Cucumber steps have driven us to create just the right code in the models. Now we have an undefined step that we need to implement. This will be the first time we actually interact with the web interface of our application. We will do this with Capybara, which is installed as part of Cucumber-Rails. Capybara will be fully explained in Chapter 15, Using Capybara to Test Ajax Web Applications. For now, let’s just use the visit(path) method that Capybara gives us. Add another step definition to features/step_definitions/user_steps.rb:

 When ​/^I visit the page for the User$/​ ​do
  expect(User.count).to eq 1
  visit(user_path(User.first))
 end

Here we are using the Rails route user_path, which takes a User instance. We’re just passing User.first since we know there is only one user in our database. Running Cucumber again now tells us that Rails doesn’t know about user_path:

 Feature: See Messages
 
  Scenario: See another user's messages
  Given there is a User
  And the User has posted the message "this is my message"
  When I visit the page for the User
  undefined method ‘user_path’ for
  #<Cucumber::Rails::World:0x63756b65> (NoMethodError)
  ./features/step_definitions/user_steps.rb:15
  features/see_messages.feature:5
  Then I should see "this is my message"
  Undefined step: "I should see "this is my message"" (Cucumber::Undefined)
  features/see_messages.feature:6
 
 Failing Scenarios:
 cucumber features/see_messages.feature:2
 
 1 scenario (1 failed)
 4 steps (1 failed, 1 undefined, 2 passed)
 0m0.136s
 
 You can implement step definitions for undefined steps with these snippets:
 
 Then(/^I should see "([^"]*)"$/) do |arg1|
  pending # Write code here that turns the phrase above into concrete actions
 end

That’s not surprising—we haven’t created a UsersController yet, and we haven’t mapped it in our Rails application’s routes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.226.240