我的应用程序中有以下模型:
class Game < ActiveRecord::Base
has_many :players
has_many :villages, :through => :players
end
class Village < ActiveRecord::Base
belongs_to :player
end
class Player < ActiveRecord::Base
belongs_to :game
has_many :villages
before_create :build_starting_village
protected
def build_starting_village
villages.build(some_attributes)
end
end
我正在使用 Shoulda/FactoryGirl 测试部分游戏功能,这是测试:
setup do
@villages = []
5.times do |i|
p = Factory(:player, :game => @game)
v = p.villages.first
assert v
@villages << v
end
assert_equal @villages.size, @game.villages.size
end
问题是最后一个断言失败。我尝试了许多丑陋的事情,例如:
@game.villages(true)
@game.players(true)
@game = Game.find(@game.id)
但我看不到问题的根源。我尝试禁用事务性固定装置(我不使用固定装置,但我认为这也会影响工厂女孩)并且它在其他测试中有所帮助,但在这里它没有效果。
设置块中的断言在大约 4 次运行中失败了 1 次。我试图怀疑 MySQL ......当我从 RubyMine 调试它时,一切都通过重新加载语句传递,但不是从命令行传递。