考慮下面的代碼的類名:軌道/ Rspec的 - 寫作規範爲belongs_to的關聯
(1)你怎麼會寫一個規範來測試HOME_TEAM和AWAY_TEAM的類名應該是一個Team類?
(2)你是否應該編寫這樣的規範?我不確定我是否看到這樣做的價值,但想要得到您的想法。
class Event < ActiveRecord::Base
belongs_to :home_team, :class_name => 'Team', :foreign_key => :home_team_id
belongs_to :away_team, :class_name => 'Team', :foreign_key => :away_team_id
end
describe Event do
it { should belong_to(:home_team) }
it { should belong_to(:away_team) }
end
將是很好,如果早該有這樣的事情:
it { should belong_to(:home_team).with_class_name(:team) }
Heads Up:除非我做了'@home_team = Team.create(...'而不是'.new('因爲它看起來像AR直到它被保存時纔會給出一個id。 – afxjzs 2013-11-12 16:00:25