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

4.0.0beta4 reports RSpec::Core::ExampleGroup::WrongScopeError Exception: default_url_options is not available on an example group #2276

Closed
samstickland opened this issue Feb 9, 2020 · 13 comments · Fixed by #2277

Comments

@samstickland
Copy link

What Ruby, Rails and RSpec versions are you using?

Ruby version: 2.6.4
Rails version: 6.0.2.1
RSpec version: 4.0.0.beta4

Observed behaviour

rspec-rails is unable to set default_url_options[:host] to ::RSpec::Rails::FeatureExampleGroup::DEFAULT_HOST as default_url_options is not defined.

Here's the output from byebug inserted just before the exception is raised:

[727, 736] in /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/example_group.rb
   727:
   728:       def self.method_missing(name, *args)
   729:         if method_defined?(name)
   730:           debugger
   731:
=> 732:           raise WrongScopeError,
   733:                 "`#{name}` is not available on an example group (e.g. a " \
   734:                 "`describe` or `context` block). It is only available from " \
   735:                 "within individual examples (e.g. `it` blocks) or from " \
   736:                 "constructs that run in the scope of an example (e.g. " \
(byebug) name
:default_url_options
(byebug) up

[13, 22] in /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-rails-4.0.0.beta4/lib/rspec/rails/example/feature_example_group.rb
   13:         app = ::Rails.application
   14:         if app.respond_to?(:routes)
   15:           include app.routes.url_helpers     if app.routes.respond_to?(:url_helpers)
   16:           include app.routes.mounted_helpers if app.routes.respond_to?(:mounted_helpers)
   17:
=> 18:           default_url_options[:host] ||= ::RSpec::Rails::FeatureExampleGroup::DEFAULT_HOST
   19:         end
   20:       end
   21:
   22:       # Shim to check for presence of Capybara. Will delegate if present, raise
(byebug) default_url_options
*** RSpec::Core::ExampleGroup::WrongScopeError Exception: `default_url_options` is not available on an example group (e.g. a `describe` or `context` block). It is only available from within individual examples (e.g. `it` blocks) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc).

nil
(byebug) ::RSpec::Rails::FeatureExampleGroup::DEFAULT_HOST
"www.example.com"

Expected behaviour

default_url_options[:host] is set correctly.

This error does not happen on 4.0.0beta3

Can you provide an example app?

Not right now, sorry.

@pirj
Copy link
Member

pirj commented Feb 9, 2020

Can you please provide a minimized version of the spec file to reproduce this?

@JonRowe
Copy link
Member

JonRowe commented Feb 9, 2020

Can you provide the spec that triggers this? Don't mind if you scrub the names etc, just looking for the RSpec structure. A stacktrace would be equally helpful, default_url_options should be defined on Rails 6 and thus this should work, we removed an if statement around this in the latest beta as it should always be present.

@eugeneius
Copy link
Contributor

I also ran into this when upgrading from 4.0.0.beta3 to 4.0.0.beta4. I've opened #2277 which includes a minimised version of the problematic example group from my application as a regression test.

@samstickland
Copy link
Author

The PR from @eugeneius pointed me towards the problem. My spec was this:

  require 'rails_helper'

  describe 'assignment agreed' do
    # let variable setup removed

    feature 'notifications' do

This gave the error on the feature 'notifications' do line (stacktrace below).

Change the outer describe block to feature resolved the problem.

The full rspec output is below. ./spec/integration/assignment_live_spec.rb:15 in the stacktrace is the feature 'notifications' do in the spec above.

[Declarative] Defaults#merge! and #call still accept arrays and automatically prepend those. This is now deprecated, you should replace `ary` with `Declarative::Variables::Append(ary)`.
DEPRECATION WARNING: The factory_girl gem is deprecated. Please upgrade to factory_bot. See https://github.com/thoughtbot/factory_bot/blob/v4.9.0/UPGRADE_FROM_FACTORY_GIRL.md for further instructions. (called from <top (required)> at /Users/samuelstickland/development/hubbado_core/config/application.rb:8)

An error occurred while loading ./spec/integration/assignment_live_spec.rb.
Failure/Error:
    feature 'notifications' do
      before do
        assignment.add_permission user: approver

        allow(Assignments::CreateAssignmentContract).to receive(:call)
          .and_return(FileWrapper.new('DEADBEEF', 'DEADBEEF', 'DEADBEEF'))
      end

      it 'sends emails when Assignment is agreed and cancelled' do
        expect {

  `default_url_options` is not available on an example group (e.g. a `describe` or `context` block). It is only available from within individual examples (e.g. `it` blocks) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc).
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/example_group.rb:730:in `method_missing'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/activesupport-6.0.2.1/lib/active_support/concern.rb:122:in `class_eval'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/activesupport-6.0.2.1/lib/active_support/concern.rb:122:in `append_features'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/configuration.rb:2306:in `include'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/configuration.rb:2306:in `safe_include'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/configuration.rb:2268:in `block in configure_group_with'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/configuration.rb:2267:in `each'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/configuration.rb:2267:in `configure_group_with'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/configuration.rb:1513:in `configure_group'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/example_group.rb:435:in `set_it_up'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/example_group.rb:386:in `subclass'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/example_group.rb:260:in `block in define_example_group_method'
# ./spec/integration/assignment_live_spec.rb:15:in `block in <top (required)>'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/example_group.rb:387:in `module_exec'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/example_group.rb:387:in `subclass'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/example_group.rb:260:in `block in define_example_group_method'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/dsl.rb:43:in `block in expose_example_group_alias'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/dsl.rb:84:in `block (2 levels) in expose_example_group_alias_globally'
# ./spec/integration/assignment_live_spec.rb:3:in `<top (required)>'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/configuration.rb:2076:in `load'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/configuration.rb:2076:in `load_file_handling_errors'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/configuration.rb:1583:in `block in load_spec_files'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/configuration.rb:1581:in `each'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/configuration.rb:1581:in `load_spec_files'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/runner.rb:102:in `setup'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/runner.rb:86:in `run'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/runner.rb:71:in `run'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/lib/rspec/core/runner.rb:45:in `invoke'
# /Users/samuelstickland/.gem/ruby/2.6.4/gems/rspec-core-3.9.1/exe/rspec:4:in `<top (required)>'
# /Users/samuelstickland/.gem/ruby/2.6.4/bin/rspec:23:in `load'
# /Users/samuelstickland/.gem/ruby/2.6.4/bin/rspec:23:in `<main>'

@pirj
Copy link
Member

pirj commented Feb 10, 2020

For specs residing in spec/integration, type: :request metadata is set.

So there are two problems with this code:

  • feature specs reside in spec/integration instead of spec/features
  • specs are using non-idiomatic describe/feature example group scheme instead of feature/scenario

Related doc.

Do you think this is something that rspec-rails needs to consider and allow for?

@JonRowe
Copy link
Member

JonRowe commented Feb 10, 2020

feature specs reside in spec/integration instead of spec/features

Thats not a problem, magic spec directories can be turned off.

@pirj
Copy link
Member

pirj commented Feb 10, 2020

That's not a problem, magic spec directories can be turned off.

Agree. Another option to resolve the issue is to override type metadata if your project structure is deliberately using spec/integration for feature specs:

RSpec.configure do |config|
  config.define_derived_metadata(file_path: %r{/spec/integration/}) do |metadata|
    metadata[:type] = :feature
  end
end

@rspec rspec deleted a comment from JonRowe Feb 10, 2020
@JonRowe
Copy link
Member

JonRowe commented Feb 10, 2020

I'd just turn them off, it'll get removed at some point.

@JonRowe
Copy link
Member

JonRowe commented Feb 10, 2020

I'm in two minds about the nested feature situation, its a bit weird, but it is just an alias for describe.

@pirj
Copy link
Member

pirj commented Feb 10, 2020

feature alias seems to be carrying type: :feature with it.

@JonRowe
Copy link
Member

JonRowe commented Feb 10, 2020

Yes thats by design, my point is theres no reason why you can't group a couple of features into one file, it just wasn't intended to be used like that

@pirj
Copy link
Member

pirj commented Feb 10, 2020

@eugeneius points out that if the outer example group has type: :request, default_url_options is defined as an instance method, and due to this check it's not defined as a class method.

Wondering if the issue is reproducible if config.infer_spec_type_from_file_location! line is commented out.

@samstickland
Copy link
Author

samstickland commented Feb 11, 2020 via email

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

Successfully merging a pull request may close this issue.

4 participants