Repository: rails/rails
File: actionview/test/template/render_test.rb (Ruby)

Code before:

    view_paths = ActionController::Base.view_paths
    assert_equal ActionView::FileSystemResolver, view_paths.first.class

    ActionView::PartialRenderer.collection_cache = ActiveSupport::Cache::MemoryStore.new

    setup_view(view_paths)
  end

  test "template body written to cache" do
    customer = Customer.new("david", 1)
    key = cache_key(customer, "test/_customer")
    assert_nil ActionView::PartialRenderer.collection_cache.read(key)
    @view.render(partial: "test/customer", collection: [customer], cached: true)
    assert_equal "Hello: david", ActionView::PartialRenderer.collection_cache.read(key)
  end

  test "template body written to cache with expiration when expires_in set" do
    customer = Customer.new("jarrett", 2)
    key = cache_key(customer, "test/_customer")
    assert_nil ActionView::PartialRenderer.collection_cache.read(key)
    @view.render(partial: "test/customer", collection: [customer], cached: true, expires_in: 1.hour)
    assert_equal "Hello: jarrett", ActionView::PartialRenderer.collection_cache.read(key)
    cache_data = ActionView::PartialRenderer.collection_cache.instance_variable_get("@data")
    cache_key = cache_data.detect { |k, _v| k.include?("jarrett") }.last
    assert_predicate(cache_key.expires_at, :present?)
  end

  test "collection caching does not cache by default" do
    customer = Customer.new("david", 1)
    key = cache_key(customer, "test/_customer")

    ActionView::PartialRenderer.collection_cache.write(key, "Cached")

    assert_not_equal "Cached",
      @view.render(partial: "test/customer", collection: [customer])
  end

  test "collection caching does not cache if controller doesn't respond to perform_caching" do
    @view.controller = nil

    customer = Customer.new("david", 1)
    key = cache_key(customer, "test/_customer")

    ActionView::PartialRenderer.collection_cache.write(key, "Cached")

    assert_not_equal "Cached",
      @view.render(partial: "test/customer", collection: [customer], cached: true)
  end

  test "collection caching does not cache if perform_caching is disabled" do

Review comment (performance):
I would drop the `instance_variable_get`. 
Test should not reach into objects like this when possible and I don't think it's necessary here. Testing that the cache expired after traveling an hour should be enough.
Here's how I would write the test  using the Arrange-Act-Assert pattern:

```suggestion
    customer = Customer.new("jarrett", 2)
    key = cache_key(customer, "test/_customer")
    @view.render(partial: "test/customer", collection: [customer], cached: true, expires_in: 1.hour)
    assert_equal "Hello: jarrett", ActionView::PartialRenderer.collection_cache.read(key)

    travel 2.hours

    assert_nil ActionView::PartialRenderer.collection_cache.read(key)

```

Code after:

    assert_nil ActionView::PartialRenderer.collection_cache.read(key)
  end

  test "collection caching does not cache by default" do
    customer = Customer.new("david", 1)
    key = cache_key(customer, "test/_customer")

    ActionView::PartialRenderer.collection_cache.write(key, "Cached")

    assert_not_equal "Cached",
      @view.render(partial: "test/customer", collection: [customer])
  end

  test "collection caching does not cache if controller doesn't respond to perform_caching" do
    @view.controller = nil

    customer = Customer.new("david", 1)
    key = cache_key(customer, "test/_customer")

    ActionView::PartialRenderer.collection_cache.write(key, "Cached")

    assert_not_equal "Cached",
      @view.render(partial: "test/customer", collection: [customer], cached: true)
  end

  test "collection caching does not cache if perform_caching is disabled" do
    @view.controller.perform_caching = false

    customer = Customer.new("david", 1)
    key = cache_key(customer, "test/_customer")

    ActionView::PartialRenderer.collection_cache.write(key, "Cached")

    assert_not_equal "Cached",
      @view.render(partial: "test/customer", collection: [customer], cached: true)
  end

  test "collection caching with partial that doesn't use fragment caching" do
    customer = Customer.new("david", 1)
    key = cache_key(customer, "test/_customer")

    ActionView::PartialRenderer.collection_cache.write(key, "Cached")

    assert_equal "Cached",
      @view.render(partial: "test/customer", collection: [customer], cached: true)
  end

  test "collection caching with cached true" do
    customer = CachedCustomer.new("david", 1)
    key = cache_key(customer, "test/_cached_customer")