Skip to content

Instantly share code, notes, and snippets.

@bcardarella
Created August 21, 2011 19:05
Show Gist options
  • Save bcardarella/1161004 to your computer and use it in GitHub Desktop.
Save bcardarella/1161004 to your computer and use it in GitHub Desktop.
Given, When, Then pattern applied to Capybara Request DSL
require 'spec_helper'
feature 'Report is Now'
scenario 'with valid attributes' do
Given 'I am authenticated' do
@user = authenticate(create(:user))
end
And 'I have no reports' do
@user.reports.should be_empty
end
When 'I post valid report data' do
post url_for([:api, :v1, :reports]), :report => { :title => 'My Report' }, :format => :json
end
Then 'I should now have a report' do
@user.reports.reload
@user.reports.should_not be_empty
end
And 'the API responds with OK' do
response.status.should be(200)
end
end
end
def AcceptanceCondition(description = '', &block)
example.metadata[:description] << "\n\s\s\s\s#{description}" if description.to_s.strip.size > 0
if block_given?
yield
else
pending 'This step has not be written yet'
end
end
def Given(description = '', &block)
AcceptanceCondition("Given #{description}", &block)
end
def When(description = '', &block)
AcceptanceCondition("When #{description}", &block)
end
def Then(description = '', &block)
AcceptanceCondition("Then #{description}", &block)
end
def And(description = '', &block)
AcceptanceCondition("And #{description}", &block)
end
def But(description = '', &block)
AcceptanceCondition("But #{description}", &block)
end
> rspec spec --format documentation
New Report It Now
with valid attributes
Given I am authenticated
And I have no reports
When I post valid report data
Then I should now have a report
And the API responds with OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment