cucumbers & factory girls
DESCRIPTION
Presentation I gave at the October Sydney RORO meetup on Cucumber, the new Ruby library for writing executable story-based tests; and FactoryGirl, a library for creating valid AR models for tests/specs/stories. Most examples come from the Webjam codebase which can be found on github: http://github.com/webjam/webjam/ Video of the preso can be found here: http://www.viddler.com/explore/snapperwolf/videos/7 Faces preso page here: http://faces.rubyonrails.com.au/groups/sydney/meetings/2008-10-08/presos/2TRANSCRIPT
CUCUMBERS! AND FACTORY GIRLS
CUCUMBER
STORIES
210 SCENARIOS
BIG BULLSHIT NUMBERS
In order to show the world how cool they areA userShould be able to edit and update their personal details
RSPEC STORIES
As a userI want to edit and update my personal detailsSo that I can show the world how cool I am
OLD SCHOOL
PHOTO BY:flickr.com/people/rumberodesign
GOOD OLD SCHOOL
BAD OLD SCHOOL
BENEFITS OF CUCUMBER?
HIGH WATER CONTENT
SKIN IS RICH IN FIBER
REGULATE BLOOD PRESSURE
RUBY + CUCUMBERS?
JAPAN + CUCUMBERS?
http://www.youtube.com/watch?v=U3OxKdDxkpg
http://www.youtube.com/watch?v=fBsyBjQuX6E
http://github.com/aslakhellesoy/cucumber/
DEMO: WEBJAM
http://github.com/webjam/webjam - rake features
/features
/features/*.feature
Story: Viewing the home page
As a visitor I want to visit the home page So that I can see all the cool stuff Scenario: not logged in Given I am not logged in When I view the home page Then I see the page
/features/home.feature
/features/steps
/features/steps/*_steps.rb
When "I view the home page" do get home_pathend
/features/home_steps.rb
Then "I see the page" do response.code.should == "200"end
/features/response_steps.rb
FEATURES
RUBY
RAILS INTEGRATION
TREETOP
STORY RUNNER COMPATIBLE
FIT TABLES
Feature: Addition In order to avoid silly mistakes As a math idiot I want to be told the sum of two numbers Scenario: Add two numbers Given I have entered 50 into the calculator And I have entered 70 into the calculator When I press add Then the result should be 120 on the screen And the result class should be Fixnum | input_1 | input_2 | button | output | class | | 20 | 30 | add | 50 | Fixnum | | 2 | 5 | add | 7 | Fixnum | | 0 | 40 | add | 40 | Fixnum |
Before do @calc = Calculator.newend After doend Given "I have entered $n into the calculator" do |n| @calc.push n.to_iend When /I press (\w+)/ do |op| @result = @calc.send opend Then /the result should be (.*) on the screen/ do |result| @result.should == result.to_fend Then /the result class should be (\w*)/ do |class_name| @result.class.name.should == class_nameend
Before do @calc = Calculator.newend After doend Given "I have entered $n into the calculator" do |n| @calc.push n.to_iend When /I press (\w+)/ do |op| @result = @calc.send opend Then /the result should be (.*) on the screen/ do |result| @result.should == result.to_fend Then /the result class should be (\w*)/ do |class_name| @result.class.name.should == class_nameend
Before do @calc = Calculator.newend After doend Given "I have entered $n into the calculator" do |n| @calc.push n.to_iend When /I press (\w+)/ do |op| @result = @calc.send opend Then /the result should be (.*) on the screen/ do |result| @result.should == result.to_fend Then /the result class should be (\w*)/ do |class_name| @result.class.name.should == class_nameend
Before do @calc = Calculator.newend After doend Given "I have entered $n into the calculator" do |n| @calc.push n.to_iend When /I press (\w+)/ do |op| @result = @calc.send opend Then /the result should be (.*) on the screen/ do |result| @result.should == result.to_fend Then /the result class should be (\w*)/ do |class_name| @result.class.name.should == class_nameend
Feature: Addition In order to avoid silly mistakes As a math idiot I want to be told the sum of two numbers Scenario: Add two numbers Given I have entered 50 into the calculator And I have entered 70 into the calculator When I press add Then the result should be 120 on the screen And the result class should be Fixnum | input_1 | input_2 | button | output | class | | 20 | 30 | add | 50 | Fixnum | | 2 | 5 | add | 7 | Fixnum | | 0 | 40 | add | 40 | Fixnum |
Feature: Addition In order to avoid silly mistakes As a math idiot I want to be told the sum of two numbers Scenario: Add two numbers Given I have entered 50 into the calculator And I have entered 70 into the calculator When I press add Then the result should be 120 on the screen And the result class should be Fixnum | input_1 | input_2 | button | output | class | | 20 | 30 | add | 50 | Fixnum | | 2 | 5 | add | 7 | Fixnum | | 0 | 40 | add | 40 | Fixnum |
GETTING STARTED WITH RAILS
INSTALL PLUGIN
./script/generate cucumber
FACTORY GIRL
INSTANTIATING MODELS
FIXTURELESS
SPECS and/or STORIES
LACHIE (LACHIE COX)
HORNSBY
NOTAHAT (PETE YANDELL)
MACHINIST
AGAIN, WEBJAMhttp://github.com/webjam/webjam/tree/master/spec/factories.rb
Factory.define :presentation, :class => Jam do |p| p.title 'Preso title' p.description 'Preso description' p.number {Factory.next(:jam_number)} p.users {|u| [u.association(:user)]} p.association :eventend
When "I view an event presentation page" do @event = Factory.create(:past_event) @presentation = Factory.create(:presentation, :event => @event) get event_presentation_path(@event, @presentation)end
ASSOCIATIONS
SEQUENCES
TIM LUCASTWITTER.COM/TOOLMANTIM
TOOLMANTIM.COM