如何使用 test::unit 和 mocha 测试 counter_cache

发布于 2024-10-10 13:29:09 字数 137 浏览 0 评论 0原文

我很好奇是否可以以某种方式模拟计数器缓存的测试。 现在我正在创建所有记录来测试 counter_cache。有什么好的做法可以有效地测试计数器缓存吗?

编辑: 我只是有兴趣测试计数器缓存是否存在于某个关联上。

I'm curious if i can mock out somehow the testing of a counter cache.
Now I'm creating all the records to test the counter_cache. Is there any good practices to test counter cache effectively?

Edit:
I'm just interested in testing that, the counter cache exists on a certain association.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

网白 2024-10-17 13:29:09

添加 :counter_cache 选项时,ActiveRecord 定义 2 个回调 (源代码)。您可以使用 Mocha 检查回调是否执行。

instance.expects(:belongs_to_counter_cache_after_create_for_MODELNAME).returns(true)

When you add the :counter_cache option, ActiveRecord defines 2 callbacks (source code). You can use Mocha to check if the callback is executed.

instance.expects(:belongs_to_counter_cache_after_create_for_MODELNAME).returns(true)
世界和平 2024-10-17 13:29:09

添加一个条目,使用 counter_cache 重新加载对象并检查它是否递增。

Add an entry, reload your object with counter_cache and check if it's increment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文