jdliss / shoulda-callback-matchers

Test existence of your Rails callbacks without having to call them
MIT License
134 stars 18 forks source link

Can't get it working with spring gem #17

Closed vfonic closed 6 years ago

vfonic commented 8 years ago

Hi,

Thanks for great gem!

I've been having troubles using this gem locally as, together with spring gem (spring rspec or just rspec), I get errors, while without it (bundle exec rspec), it works as expected.

Here's the error message I'm getting:

Failure/Error: it { is_expected.to callback(:set_promo_code).before(:validation) }
     NoMethodError:
       undefined method `callback' for #<RSpec::ExampleGroups::Customer::Callbacks::SetPromoCode:0x007f9201ad45e0>
     # ./spec/models/customer_spec.rb:40:in `block (4 levels) in <top (required)>'
     # /Users/viktorfonic/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
     # /Users/viktorfonic/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
     # /Users/viktorfonic/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
     # /Users/viktorfonic/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
     # /Users/viktorfonic/.rvm/gems/ruby-2.2.1/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call'
     # /Users/viktorfonic/.rvm/gems/ruby-2.2.1/gems/spring-1.4.0/lib/spring/command_wrapper.rb:38:in `call'
     # /Users/viktorfonic/.rvm/gems/ruby-2.2.1/gems/spring-1.4.0/lib/spring/application.rb:183:in `block in serve'
     # /Users/viktorfonic/.rvm/gems/ruby-2.2.1/gems/spring-1.4.0/lib/spring/application.rb:156:in `fork'
     # /Users/viktorfonic/.rvm/gems/ruby-2.2.1/gems/spring-1.4.0/lib/spring/application.rb:156:in `serve'
     # /Users/viktorfonic/.rvm/gems/ruby-2.2.1/gems/spring-1.4.0/lib/spring/application.rb:131:in `block in run'
     # /Users/viktorfonic/.rvm/gems/ruby-2.2.1/gems/spring-1.4.0/lib/spring/application.rb:125:in `loop'
     # /Users/viktorfonic/.rvm/gems/ruby-2.2.1/gems/spring-1.4.0/lib/spring/application.rb:125:in `run'
     # /Users/viktorfonic/.rvm/gems/ruby-2.2.1/gems/spring-1.4.0/lib/spring/application/boot.rb:18:in `<top (required)>'
     # -e:1:in `<main>'

It looks like as if the gem is not loaded at all.

Gemfile:

group :development do
  ...
  gem 'spring'
  gem 'spring-commands-rspec'
  gem 'shoulda-matchers', require: false
  gem 'shoulda-callback-matchers'
end

Gemfile.lock:

shoulda-callback-matchers (1.1.3)
shoulda-matchers (2.8.0)
beatrichartz commented 8 years ago

Seems like class reloading in Spring might unload the matchers - I'll have a look later this week

beatrichartz commented 8 years ago

This is due to classes being redefined by Spring - currently this library does not accommodate for reloaded classes. A fix might be to load the matchers into the test library config (RSpec), which you could try with something along the lines of:

Rspec.configure do |config|
  config.include(Shoulda::Callback::Matchers::ActiveModel)
end

This could be done in the library itself as well, although the inclusion into test libraries would have to be reworked for that. Help is very welcome.

vfonic commented 8 years ago

Thanks @beatrichartz !

I've got it working by including the matchers in RSpec.config as you suggested. I've also created a PR with this information added to Readme.md as I believe Spring is widely used: https://github.com/beatrichartz/shoulda-callback-matchers/pull/19