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

Template stubs are not removed in Rails 7.1. #2696

Closed
tristandunn opened this issue Oct 12, 2023 · 4 comments · Fixed by #2714
Closed

Template stubs are not removed in Rails 7.1. #2696

tristandunn opened this issue Oct 12, 2023 · 4 comments · Fixed by #2714

Comments

@tristandunn
Copy link

Note that #2644 feels a little similar, but didn't have any luck with the suggested fix with this issue.

I did a good bit of debugging and can definitely see the resolver still listed in the view paths. I tried a few different ways to remove it manually without any luck. Also tried recreating the view paths, but I don't think I have enough Rails context to truly determine how to fix it.

What Ruby, Rails and RSpec versions are you using?

Ruby version: 3.2.2
Rails version: 7.1.0
RSpec version: main

Observed Behavior

The template stub isn't removed so the view isn't rendered correctly resulting in a test failure.

$ ruby app.rb --seed 11372 --format doc

Randomized with seed 11372

_example.html.erb
  renders the stub template
  renders the file template (FAILED - 1)

Failures:

  1) _example.html.erb renders the file template
     Failure/Error: expect(html).to include("TEMPLATE_HTML")

       expected #<ActionView::TestCase::Behavior::Content("STUB_HTML")> to include "TEMPLATE_HTML"
       Diff:
       @@ -1 +1 @@
       -["TEMPLATE_HTML"]
       +"STUB_HTML"

     # app.rb:36:in `block (2 levels) in <main>'

Finished in 0.01424 seconds (files took 0.3224 seconds to load)
2 examples, 1 failure

Expected Behavior

The template stub is removed so the view is rendered correctly.

$ ruby app.rb --seed 34554 --format doc

Randomized with seed 34554

_example.html.erb
  renders the file template
  renders the stub template

Finished in 0.00953 seconds (files took 0.32798 seconds to load)
2 examples, 0 failures

Can you provide an example app?

app.rb

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "rails", "7.1.0"

  gem "rspec-core",         github: "rspec/rspec-core", branch: "main"
  gem "rspec-expectations", github: "rspec/rspec-expectations", branch: "main"
  gem "rspec-mocks",        github: "rspec/rspec-mocks", branch: "main"
  gem "rspec-support",      github: "rspec/rspec-support", branch: "main"
  gem "rspec-rails",        github: "rspec/rspec-rails", branch: "main"
end

require "rails"
require "action_controller/railtie"
require "action_view/railtie"

class MyApplication < Rails::Application; end

Rails.application.initialize!

require "rspec/rails"
require "rspec/autorun"

describe "_example.html.erb", type: :view do
  subject(:html) do
    render partial: "example"

    rendered
  end

  it "renders the file template" do
    expect(html).to include("TEMPLATE_HTML")
  end

  it "renders the stub template" do
    stub_template("_example.html.erb" => "STUB_HTML")

    expect(html).to include("STUB_HTML")
  end
end

app/views/_example.html.erb

TEMPLATE_HTML
@jrochkind
Copy link
Contributor

jrochkind commented Nov 7, 2023

Hi, I just ran into this too.

After FIRST discovering I needed a stub_template implementation that is not yet in an rspec-rails release (from commit 4d65bea) for Rails 7.1, I THEN discovered that even with this implementation it is subject to this problem here.

I may have found an alternate build_template implementation that works however, using only Rails 7.1 public API instead of the current patchy implementation.

  module RSpec
    module Rails
      module ViewExampleGroup
        module ExampleMethods
          def stub_template(hash)
            controller.prepend_view_path(StubResolverCache.resolver_for(hash))
          end
        end
      end
    end
  end

Changing the source code changes the order of tests for a given seed, so I needed a different seed to get the tests to run in repro order. (I also had to add timeout "0.4.1" to inline gemfile to make bundler happy, don't know why). So I had to use different seed value to get teh tests to run in proper repro order.

But with patched stub_template above, tests run in proper order (first stub_template then file_template) is green.

@tristandunn thanks for the repro script, interested in checking this out and confirming?

I am not certain if this will solve things everywhere (is controller available in every context stub_template can be used?)? And I'm not sure how to test this in rspec's own test suite to turn this into a PR.

Feedback?

jrochkind added a commit to projectblacklight/blacklight that referenced this issue Nov 7, 2023
jrochkind added a commit to projectblacklight/blacklight that referenced this issue Nov 7, 2023
@tristandunn
Copy link
Author

@jrochkind Using main and your patch has my project green.

@JonRowe
Copy link
Member

JonRowe commented Nov 21, 2023

Fix technically released in 6.0.4 but please use 6.1.0 for Rails 7.1 support.

@jrochkind
Copy link
Contributor

Thank you!

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

Successfully merging a pull request may close this issue.

4 participants