views:

66

answers:

1

I am trying to create some cucumber features with references to other objects. I am running into some problems when I want to create relationships between objects and run tests on them. I haven't found any samples anywhere else.

Let's say I have the following models:

class Athlete < ActiveRecord::Base
  belongs_to :shoe_size
  validates_presence_of :first_name
  validates_presence_of :last_name
  validates_presence_of :shoe_size_id
end

And

class Shoe_Size < ActiveRecord::Base
end

That correspond to

create_table :athlete do |t|
  t.string :first_name
  t.string :last_name
  t.references :shoe_size
end

And

create_table :shoe_size do |t|
  t.string :size
  t.integer :sort_order
end

In Cucumber, I might create the following feature:

Scenario: Follow the edit link from the athlete list
    Given the following shoe sizes
          | size | sort_order |
          | 10   | 1          |
          | 10.5 | 2          |
          | 11   | 3          |
      And the following athlete
          | first_name | last_name | size |
          | John       | Doe       | 10.5 |
      And I am on the list page
     When I follow "edit"
     Then I should be on the edit page

I'd like to be able to give data like "Size" because I don't know the :id of the shoe sizes created in the first given. Is this the right approach to the problem? Should I do something else?

How do I set up my step definition to do this? I tried something like the following, but it is nowhere close.

Given /^the following athlete$/ do |table|
  table.hashes.each do |hash|
    shoe_size = hash[:shoe_size]
    hash.delete(:shoe_size)
    hash[:shoe_size_id] = Radius.find(:size => radius_size).id
    athlete.create(hash)
  end
end
A: 
Given /^the following athlete$/ do |table|
  table.hashes.each do |hash|
    Athlete.create(:first_name => hash[:first_name],
      :last_name => hash[:last_name]
      :shoe_size => ShoeSize.find_by_size(hash[:size])
  end
end

ought to work, although you might want to look at using fixtures or factories if you don't really need to describe the model attributes in the scenario. Factory Girl might be a good fit.

zetetic