roro facebook

roro Sydney , October 8th, 2008 19:00

Cucumbers and Factory Girls by toolmantim

You can see and download the slides from slideshare, or watch the full vid and audio recording on viddler.

Cucumber

An intro to stories using cucumber, a tool for executing human readable stories. I mentioned how to replace Rails integration tests and rspec stories with cucumber stories and demoed executing the stories in the webjam codebase.

Cucumber is set to be the defacto standard and RSpec story runner will be deprecated by the RSpec team.

To get up and running with cucumber in a Rails app you simply install it from the github repo as a plugin, run:

1
./script/generate cucumber

and then run the rake task:

1
rake features

An example story from webjam’s posts.feature

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Feature: Viewing post pages

  In order to absorb Lachlan's deep insights into man and machine
  I should be able to visit pages and pages and pages of blog posts
  
  Scenario: viewing a post when logged in
    Given I am logged in
    When I view the post page
    Then I see the page

  Scenario: viewing a post when not logged in
    Given I am not logged in
    When I view the post page
    Then I see the page

  Scenario: viewing a mobile post when logged in
    Given I am logged in
    When I view the mobile post page
    Then I see the page
  
  Scenario: viewing a mobile post when not logged in
    Given I am logged in
    When I view the mobile post page
    Then I see the page

You’ll notice the format is a bit different to RSpec story format though don’t worry, Cucumber is backwards compatible. Moving to the new format is easy though, see webjam changeset dd8a992: Change to the new cucumber story format for an example.

factory_girl

factory_girl is a ruby lib to help instantiate AR models for testing data. It bypasses validations, supports sequences for generating unique values and can reflect and assign AR associations. It’s useful in both specs and stories to instantiate valid object models.

Example from the webjam factories file

1
2
3
4
5
6
7
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 :event
end

To use your factories simply require the factories file. For cucumber stories you can do this from your features/steps/env.rb.

p.s. this faces preso stuff rocks… lachie rocks… it all ROCKS

rating (from 1 rating)

length
(3.00)
content
(3.00)
slides
(2.00)

you reckon...

Hey, what were those videos you said to watch? I watched the “test all the fucking time one”, what’s the other?

→ dylanfm · 2 months ago
log in to comment & rate.