Wednesday, July 29, 2009

Building a merb application, part two

In part 1 we got our requirements and started building our first cucumber feature. We didn’t finish it so lets do that now.

First job is the enhance the feature. We will have two scenarios now, one with a successful login, and one that accepts an invite. However we are going to get a lot repetition if we have to have all those log in steps for each feature. Luckily we can call steps from within steps. Modify the setup_steps.rb as follows. We have also filled in the I have an invite step.

Given /^I have an invite$/ do
@user.invites.build if @user.invites.empty?
end

Given /^I am logged in as "(.*)" with password "(.*)"$/ do |login,password|
Given "I am not authenticated"
And "I am a user \"#{login}\" with password \"#{password}\""
And "I have an invite"
And "I go to /invites"
And "I fill in \"login\" with \"#{login}\""
And "I fill in \"password\" with \"#{password}\""
And "I press \"Log In\""

end




Now our feature looks like this





  Scenario: Login 
Given I am logged in as "myfriend" with password "apassword"
Then I should see "Are you coming?"

Scenario: Accept Invite
Given I am logged in as "myfriend" with password "apassword"
When I press "I'm coming!"
Then I should see "Accepted invite!"




To pass the new feature we need to update the application.

First we need to add an accepted property to the Invite model. We’ll actually make this a datetime so we know when they accepted.


Add this line to models/invite.rb, just under the belongs_to statement.





  property :accepted, DateTime




And in controllers/invites.rb, lets limit the invites to the current user.





  def index
@invites = session.user.invites
display @invites
end



In views/invites/index.html.erb, add this



<% invite = @invites.first %>
<%= form_for invite, :action => url(:invite, invite) do %>
<%= hidden_field :accepted, :value=>DateTime.now %>
<%= submit "I'm coming!" %>
<% end =%>




Note that we are only using the first invite, because that’s all the feature requires to pass. For BDD, we write the absolute minimum to pass the tests. Now run our features again. We should get all green!

> rake features


Part three will implement multiple invites.


To get the code clone the repository (if you haven’t already done so) and check out this commit.


> git clone git://github.com/donibuchanan/merb-rsvp.git


> cd merb-rsvp


> git checkout 67d5ebde343024baac5f0c5a704c5b3719f5310d

No comments:

Post a Comment