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

stub_template isn't working as expected on latest version #2722

Closed
wildmaples opened this issue Dec 11, 2023 · 1 comment
Closed

stub_template isn't working as expected on latest version #2722

wildmaples opened this issue Dec 11, 2023 · 1 comment

Comments

@wildmaples
Copy link

wildmaples commented Dec 11, 2023

What Ruby, Rails and RSpec versions are you using?

Ruby version: 3.2
Rails version: 7.0
RSpec version: 6.1.0 (latest)

Observed behaviour

Hi! We are trying to use the latest version of rspec-rails. When requiring RSpec::Rails::ViewExampleGroup::ExampleMethods to use stub_template, we are seeing the following error:



  1) Foo does something
     Failure/Error: stub_template('test/test_email.html.erb' => '<h1>Test email!</h1>')
     
     NameError:
       undefined local variable or method `controller' for #<RSpec::ExampleGroups::Foo "does something" (./emails/spec/testing_spec.rb:11)>
     # ./packs/emails/spec/testing_spec.rb:8:in `block (2 levels) in <main>'
     # -e:1:in `<main>'

Expected behaviour

I would expect stub_template to work. I believe the changes in this PR caused the issue: #2714

Can you provide an example reproduction?

Yep! This is the code to reproduce the error:

class Foo; end

RSpec.describe Foo do
  include RSpec::Rails::ViewExampleGroup::ExampleMethods
  before do
    stub_template('test/test_email.html.erb' => '<h1>Test email!</h1>')
  end

  it 'does something' do
    expect(1).to eq(1)
  end
end
@JonRowe JonRowe closed this as completed Dec 12, 2023
@JonRowe
Copy link
Member

JonRowe commented Dec 12, 2023

👋 Please use the public api include ::RSpec::Rails::ViewExampleGroup or include via tags which is the preferred method. RSpec.describe "Something", type: :view.

You'll notice that in the view there are several things in view specs that depend on controller as a method because thats how Rails does things internally, by bringing in only the DSL module, you're skipping all the setup that makes it work,

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

2 participants