Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing with frozen time does not work #427

Open
svobom57 opened this issue May 16, 2023 · 0 comments
Open

Testing with frozen time does not work #427

svobom57 opened this issue May 16, 2023 · 0 comments

Comments

@svobom57
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant