Rails Factory Girl在规范和交易中回滚

时间:2013-01-30 07:19:59

标签: rspec transactions factory-bot

我正在测试我的群组模型上的方法。它依赖于通过工厂创建一些徽章类型。

这是代码

it "add itself to badges with method add_to_badgetypes" do
    badge = FactoryGirl.create(:badgetype)
    old_badge_ids = badge.group_ids.split(",")
    badge2 = FactoryGirl.create(:badgetype, :group_ids => "3,4,5")
    group.add_to_badgetypes(badge.id, badge2.id)
    expect( badge.group_ids ).to eq( old_badge_ids.push(group.id).join(",") ) 
end

badge.group_ids'之前是“200,300”,之后应该是“200,300,#{group.id}”。当我拖尾日志时,我看到了这个:

  `   Badgetype Load (0.5ms)  SELECT group_ids FROM `badge_types` WHERE `badge_types`.`id` = 40 LIMIT 1

   (0.2ms)  SAVEPOINT active_record_1

   (0.3ms)  UPDATE `badge_types` SET `group_ids` = '200,300,15' WHERE `badge_types`.`id` IS NULL

   (0.2ms)  RELEASE SAVEPOINT active_record_1

  Badgetype Load (0.4ms)  SELECT group_ids FROM `badge_types` WHERE `badge_types`.`id` = 41 LIMIT 1

   (0.1ms)  SAVEPOINT active_record_1

   (0.3ms)  UPDATE `badge_types` SET `group_ids` = '3,4,5,15' WHERE `badge_types`.`id` IS NULL

   (0.1ms)  RELEASE SAVEPOINT active_record_1

   (0.3ms)  ROLLBACK

   (0.1ms)  BEGIN

`

因此它成功更新了group_ids,但由于某种原因,测试失败了,如果我pp badge.group_ids我得到原始值(200,300),而不是更新的值。

我认为这与'SAVEAPOINT active_record_1'有关,然后是UPDATE,然后是RELEASE SAVEPOINT。如果是这样,为什么?

我怎样才能通过这个规范,因为它似乎在数据库中搞乱,而不是方法。

由于

修改

通过在控制台中玩游戏,找到更多有趣的信息。如果我创建一个工厂并将其分配给一行:

g = FactoryGirl.create(:group)

然后运行:

g.add_to_badgetype(1,2)

我得到这个SQL

1.9.3-p286 :080 > g.add_to_badgetypes(1, 2)
  Badgetype Load (0.5ms)  SELECT group_ids FROM `badge_types` WHERE `badge_types`.`id` = 1 LIMIT 1
   (0.1ms)  BEGIN
   (0.2ms)  UPDATE `badge_types` SET `group_ids` = 'test,8,9,10' WHERE `badge_types`.`id` IS NULL
   (0.1ms)  COMMIT
  Badgetype Load (0.3ms)  SELECT group_ids FROM `badge_types` WHERE `badge_types`.`id` = 2 LIMIT 1
   (0.1ms)  BEGIN
   (0.2ms)  UPDATE `badge_types` SET `group_ids` = '200,300,8,9,10' WHERE `badge_types`.`id` IS NULL
   (0.1ms)  COMMIT
 => [1, 2] 

注意奇数UPDATE语句“WHERE badge_types.id IS NULL”

如果在我创建工厂后,我将其重新分配给变量然后运行相同的命令,它可以找到:

1.9.3-p286 :081 > g = Group.last
  Group Load (0.5ms)  SELECT `groups`.* FROM `groups` ORDER BY `groups`.`id` DESC LIMIT 1
 => #<Group id: 10, group_name: "Test Group", short_name: "test_group", enrollment: false, kiosk: false, landing: 0, ro_landing: 0> 
1.9.3-p286 :082 > g.add_to_badgetypes(1, 2)
  Badgetype Load (0.6ms)  SELECT `badge_types`.* FROM `badge_types` WHERE `badge_types`.`id` = 1 LIMIT 1
   (0.1ms)  BEGIN
   (0.4ms)  UPDATE `badge_types` SET `group_ids` = 'test,8,9,10' WHERE `badge_types`.`id` = 1
   (0.2ms)  COMMIT
  Badgetype Load (0.4ms)  SELECT `badge_types`.* FROM `badge_types` WHERE `badge_types`.`id` = 2 LIMIT 1
   (0.1ms)  BEGIN
   (0.4ms)  UPDATE `badge_types` SET `group_ids` = '200,300,8,9,10' WHERE `badge_types`.`id` = 2
   (0.2ms)  COMMIT
 => [1, 2] 

显然,作为工厂的组正在使SQL更新语句查找NULL的ID。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

Aaaaaa我发现了。

我只需要重新加载变量“badgetype”

在测试之前,我添加了

@ badge.reload

瞧,瞧。怪异。