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

查看:21
本文介绍了可选地在 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 来说相当笨拙或风格化:如何在功能测试中启用页面缓存导轨?

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?

推荐答案

你很可能会以相互干扰的测试告终.您应该使用确保将其包装起来并适当地将其重置为旧值.一个例子:

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天全站免登陆