可以选择在Rails 3功能测试中测试缓存 [英] Optionally testing caching in Rails 3 functional tests

查看:60
本文介绍了可以选择在Rails 3功能测试中测试缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,我希望功能测试不执行动作缓存。 Rails似乎支持我,在 environment / test.rb 中默认为 config.action_controller.perform_caching = false 。这样会导致正常的功能测试没有测试缓存。

Generally, I want my functional tests to not perform action caching. Rails seems to be on my side, defaulting to config.action_controller.perform_caching = false in environment/test.rb. This leads to normal functional tests not testing the caching.

所以我该如何在Rails 3中测试缓存。

So how do I test caching in Rails 3.

此主题中提出的解决方案似乎很笨拙或对Rails 2不屑一顾:
如何在Rails的功能测试中启用页面缓存?

The solutions proposed in this thread seem rather hacky or taylored towards Rails 2: How to enable page caching in a functional test in rails?

我要执行以下操作:

test "caching of index method" do
  with_caching do
    get :index
    assert_template 'index'
    get :index
    assert_template ''
  end
end

也许还有更好的方法测试缓存是否被命中?

Maybe there is also a better way of testing that the cache was hit?

推荐答案

您可能会终止互相踩踏的测试。您应该使用sure来包装它,并适当地将其重置为旧值。示例:

You're liable to end up with tests stomping on each other. You should wrap this up with ensure and reset it to old values appropriately. An example:

module ActionController::Testing::Caching
  def with_caching(on = true)
    caching = ActionController::Base.perform_caching
    ActionController::Base.perform_caching = on
    yield
  ensure
    ActionController::Base.perform_caching = caching
  end

  def without_caching(&block)
    with_caching(false, &block)
  end
end

我也将其放入模块中,因此您可以将其包含在测试类或父类中。

I've also put this into a module so you can just include this in your test class or parent class.

这篇关于可以选择在Rails 3功能测试中测试缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆