Calling `before` or `after` in an invalid context that will never run should raise an error
schneems opened this issue · 3 comments
Expected
If I add this code to my spec_helper.rb it should either run or error:
before(:suite) do
puts "I should run here after before the whole suite"
end
after(:each) do
puts "I should run here after each"
endActual
It does nothing. It is valid code (so it doesn't raise an error), but the code inside is never executed. You can verify by changing the puts to a raise.
Ideally I would get an error (or warning if we cannot be certain) like:
Error you are using `begin(:each)` in a context where it will have no effect, ensure it's being called inside a `describe` or `RSpec.configure {|config| config.begin(:each) #...` block
Versions
$ ruby -v
ruby 3.1.4p223 (2023-03-30 revision 957bb7cb81) [x86_64-darwin22]
$ cat Gemfile.lock | grep rspec
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.6)
rspec-support (~> 3.12.0)
rspec-support (3.12.1)
rspec
Huh, this surprises me... We generally take care not to monkey patch where possible, so only certain top level methods like describe and the shared example stuff should be available, I'll investigate but no monkey patching will be the default in 4.
Can you check the source location of that method for me? I get the "correct" behaviour:
NoMethodError:
undefined method `before' for main:Object
It looks like you're right, it's coming from sinatra, not from rspec
puts method(:before)
# => #<Method: Object(Sinatra::Delegator)#before(*args, &block) /Users/rschneeman/.gem/ruby/3.1.4/gems/sinatra-3.1.0/lib/sinatra/base.rb:2036>
Thanks for checking ❤️