NoMethodError undefined method `shared_examples_for’ for main:Object for bundle gem rspecPosted: June 29, 2021 | Author: ThomasPowell | Filed under: ruby | Tags: bundle, gem, rspec, ruby | Leave a comment »
If you create a gem stub using
bundle gem thing and select
rspec as your test suite, you may get an error similar to the following:
❯ bundle exec rspec An error occurred while loading ./spec/thing_spec.rb. Failure/Error: shared_examples_for 'saying hello' do puts "hi" end NoMethodError: undefined method `shared_examples_for' for main:Object # ./spec/thing_spec.rb:1:in `<top (required)>' No examples found.
After a lot of diving into rspec source code to verify where
shared_examples_for was defined (
rspec-core so…) I noticed the following code in the stubbed
# Disable RSpec exposing methods globally on `Module` and `main` config.disable_monkey_patching!
If you comment out
config.disable_monkey_patching!, then those methods will be included at a top level.