sitemap_generator icon indicating copy to clipboard operation
sitemap_generator copied to clipboard

Testing with frozen time does not work

Open svobom57 opened this issue 1 year ago • 0 comments

Description

I have a following RSpec code to test the sitemap generation:

require "rails_helper"

RSpec.describe RefreshSitemapsJob do
  subject(:generate_sitemap) { described_class.new.perform }

  around { |spec| travel_to(Time.zone.local(2022, 5, 15, 13, 14, 15)) { spec.run } }

  def parse_xml(file_path)
    File.open(file_path) { |f| Nokogiri::XML(f) }.to_s
  end

  it "generates a sitemap", :aggregate_failures do
    generate_sitemap

    expect(parse_xml("public/sitemap.xml")).to eq(parse_xml("spec/fixtures/sitemap.xml"))
  end
end

then the worker that generates the Sitemap has a code similar to this:

class RefreshSitemapsJob
  include Sidekiq::Worker

  def perform
    SitemapGenerator::Sitemap.create do
      group(filename: :sitemap_products, max_sitemap_links: 3_000) do
        Product.find_each do |product|
          add product_path(id: product), lastmod: product.updated_at
        end
      end
    end
  end
end

The expectation fails on the following diff:

      <sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd">
          <loc>http://localhost:3000/sitemap_products.xml</loc>
     -    <lastmod>2023-05-15T13:14:15+01:00</lastmod>
     +    <lastmod>2023-05-16T11:03:23+01:00</lastmod>
        </sitemap>
      </sitemapindex>

(the time just keeps ticking normally when the sitemap generates the group.

This leads me to believe that the library is doing something that does not respect the time freeze I'm doing in the around block.

svobom57 avatar May 16 '23 10:05 svobom57