WinstonYW

Just Another Tech Journal

Projects

Elsewhere

Spring And Failing Specs

03 MARCH 2014

Spring will be included in Rails 4.1 as the defacto Application Preloader. It's similar to Zeus which keeps your application running in the background during development, so that any rake or spec command will run speedily without having to reload the app.

Anyway, I was using spring, spring-commands-rspec and enumerize gems in my app and was running specs with spring rspec when this happened:

$> spring rspec

........F.............................

Failures:

  1) Feedback validations
     Failure/Error: it { should enumerize(:emotion).in(:happy, :sad) }
     NoMethodError:
       undefined method `enumerize' for #<RSpec::Core::ExampleGroup::Nested_3::Nested_2:0x000001029bcd20>
     # ./spec/models/feedback_spec.rb:22:in `block (3 levels) in <top (required)>'
     # -e:1:in `<main>'

Finished in 0.27399 seconds
38 examples, 1 failure

However, if I just do rspec, the specs passed!

$> rspec

......................................

Finished in 0.27061 seconds
38 examples, 0 failures

Randomized with seed 2852

Dug into the problem and found that this was a loading issue with enumerize.

This was how the enumerize matchers were loaded:

if defined?(::RSpec)
  require 'enumerize/integrations/rspec'
end

And it works fine for plain rspec command because rspec loads before rails. However, when spring starts the application, rspec is not yet loaded and so the matchers will not be required as well.

The fix is to load rspec like this:

begin
  require 'rspec/matchers'
rescue LoadError
else
  require 'enumerize/integrations/rspec'
end

This makes sure that the enumerize matchers will only required when rspec/matchers can be required without errors, which means it will only load in dev or test environment because you shouldn't have rspec bundled in your production environment.

Pull request over here.

Keep this in mind when you encounter weird failing specs when using spring.

comments powered by Disqus