我正在尝试使用对其他对象的引用来创建一些黄瓜功能。当我想在对象之间创建关系并对它们运行测试时,我遇到了一些问题。我还没有在其他任何地方找到任何样品。
假设我有以下型号:
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
在Cucumber中,我可能会创建以下功能:
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
答案 0 :(得分: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可能是合适的。