1

我的应用程序中有以下模型:

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 调试它时,一切都通过重新加载语句传递,但不是从命令行传递。

4

1 回答 1

1

尝试断言真实性以外的东西,因为它可以是任何东西,例如assert_instance_of Village

在以下情况下还会发生什么?请原谅我有任何轻微的语法错误,有一段时间没有使用 test::unit/should 了,但是在你的代码中,不确定@game来自哪里

def test_player_has_village_on_new
  p = Factory(:player, :game => Factory(:game))
  assert_equal 1, p.villages.size
end
于 2010-09-11T13:55:39.957 回答