我有以下型号:
class Game < ActiveRecord::Base
validates_presence_of :team_b_id, :team_a_id
def description
Team.find(team_a_id).name + " vs " + Team.find(team_b_id).name
end
end
因为我写了这个测试:
describe Game do
...
it "returns game description" do
game = FactoryGirl.create(:game) do
team_a_id = FactoryGirl.create(:team, name: "Team A").id
team_b_id = FactoryGirl.create(:team, name: "Team B").id
end
game.description.should == team_a.name + " vs " + team_b.name
end
end
验证无效,因为它无法找到ID为1&#34的团队。 这些是工厂:
FactoryGirl.define do
factory :game do |f|
f.team_a_id { 1 }
f.team_b_id { 2 }
end
end
FactoryGirl.define do
factory :team do |f|
f.name { "Team Name" }
end
end
由于
答案 0 :(得分:0)
看起来错误可能发生在描述函数中,因为你没有正确设置id。
尝试将代码更改为以下内容:
team_a_id = FactoryGirl.create(:team, name: "Team A").id
team_b_id = FactoryGirl.create(:team, name: "Team B").id
game = FactoryGirl.create(:game, team_a_id: team_a_id, team_b_id: team_b_id)
这应该在游戏模型中设置id。