Associating a Message with a User

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

rails/08/app/models/message.rb
 
class​ Message < ActiveRecord::Base
 
belongs_to :user
 
attr_accessible :content, :user_id
 
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.445s
 
 
You can implement step definitions for undefined steps with these snippets:
 
When /^I visit the page for the User$/ do
 
pending # express the regexp above with the code you wish you had
 
end
 
 
Then /^I should see "(.*?)"$/ do |arg1|
 
pending # express the regexp above with the code you wish you had
 
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:

rails/09/features/step_definitions/user_steps.rb
 
When /^I visit the page for the User$/ ​do
 
User.count.should == 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.348s
 
 
You can implement step definitions for undefined steps with these snippets:
 
Then /^I should see "(.*?)"$/ do |arg1|
 
pending # express the regexp above with the code you wish you had
 
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
3.133.123.126