加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails 3和Rspec:计数器缓存列在预期时更新为2

发布时间:2020-12-17 02:53:22 所属栏目:百科 来源:网络整理
导读:我正在用Rspec测试一个名为Solutions的模型,它有很多喜欢.解决方案存储它有多少喜欢(counter_cache).它有一个“likes_count”属性(和相应的db字段). 当我创建一个与解决方案关联的Like记录时,我希望解决方案属性“likes_count”应该从nil更新为1.当我在控制
我正在用Rspec测试一个名为Solutions的模型,它有很多喜欢.解决方案存储它有多少喜欢(counter_cache).它有一个“likes_count”属性(和相应的db字段).

当我创建一个与解决方案关联的Like记录时,我希望解决方案属性“likes_count”应该从nil更新为1.当我在控制台中执行此操作时,它可以正常工作.

但是当我运行规范时,我在控制台中执行相同的操作,它将TWICE更新为“likes_count”字段,将其设置为2.

看一下(在控制台中)工作:

irb(main):001:0> solution = Factory(:solution)
irb(main):004:0> solution.likes_count 
=> nil
irb(main):006:0> like = Factory(:like,:likeable => solution)
=> #<Like id: 1,user_id: 2,likeable_id: 1,likeable_type: "Solution",created_at: "2011-11-23 19:31:23",updated_at: "2011-11-23 19:31:23">
irb(main):007:0> solution.reload.likes_count
=> 1

看看规格结果不工作:

1) Solution counter cache should be increased when a like is created
 Failure/Error: subject.reload.likes_count.should be 1

   expected #<Fixnum:3> => 1
        got #<Fixnum:5> => 2

   Compared using equal?,which compares object identity,but expected and actual are not the same object. Use
   'actual.should == expected' if you don't care about
   object identity in this example.
 # ./spec/models/solution_spec.rb:45:in `block (3 levels) in <top (required)>'

这是规格:

describe "counter cache" do
  let(:solution) { Factory(:solution) }

  it "should be increased when a like is created" do
    Factory(:like,:likeable => solution)
    solution.reload.likes_count.should be 1
  end
end

我看了一下test.log,我意识到更新计数器缓存列的db查询在测试中被调用了两次.

SQL (0.5ms)  INSERT INTO "likes" ("created_at","likeable_id","likeable_type","updated_at","user_id") VALUES (?,?,?)  [["created_at",Wed,23 Nov 2011 19:38:31 UTC +00:00],["likeable_id",121],["likeable_type","Solution"],["updated_at",["user_id",204]]
  SQL (0.3ms)  UPDATE "solutions" SET "likes_count" = COALESCE("likes_count",0) + 1 WHERE "solutions"."id" IN (SELECT "solutions"."id" FROM "solutions" WHERE "solutions"."id" = 121 ORDER BY id DESC)
  SQL (0.1ms)  UPDATE "solutions" SET "likes_count" = COALESCE("likes_count",0) + 1 WHERE "solutions"."id" IN (SELECT "solutions"."id" FROM "solutions" WHERE "solutions"."id" = 121 ORDER BY id DESC)
  Solution Load (0.3ms)  SELECT "solutions".* FROM "solutions" WHERE "solutions"."id" = ? LIMIT 1  [["id",121]]

解决方法

我有同样的问题.事实证明,我的spec_helper.rb第二次加载了模型,因此创建了第二个回调来更新计数器.确保您的解决方案模型未被其他进程重新加载.

上面的答案也是正确的:您需要使用==而不是进行比较,但这不会修复您在日志文件中看到的多个更新.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读