2010-01-28 56 views
1

我想创建一些黄瓜功能引用其他对象。当我想创建对象之间的关系并对它们运行测试时,我遇到了一些问题。我还没有在其他地方找到任何样本。如何在Cucumber中测试ActiveRecord引用?

比方说,我有以下型号:

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 

而且

class Shoe_Size < ActiveRecord::Base 
end 

对应于

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

而且

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

黄瓜,我可能会创建以下特点:

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 

我希望能够给数据,如“大小”,因为我不知道的:在第一个创建的鞋码的ID给出。这是解决问题的正确方法吗?我应该做点别的吗?

如何设置我的步骤定义来执行此操作?我尝试了类似下面的内容,但它并没有太接近。

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 

回答

1
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 

应该工作,虽然你可能想看看使用夹具或工厂,如果你不真的需要描述场景的模型属性。 Factory Girl可能是个不错的选择。

+0

如果有一个表Athelte但黄瓜错误是什么不能找到表运动员? – Sergey 2012-03-18 07:28:36

+1

@Sergey'rake db:test:prepare' – Ajedi32 2012-08-17 18:08:15

相关问题