Kyle Decot
Kyle Decot

Reputation: 20815

Rspec SimpleCov NoMethodError

When running my rails 4 rspec suite via rake everything works correctly but when attempting to run rake simplecov I get hundreds of failures all w/ a NoMethodError something like:

  1) Answer Validations
     Failure/Error: it { should validate_presence_of :text }
     NoMethodError:
       undefined method `validate_presence_of' for #<RSpec::Core::ExampleGroup::Nested_16::Nested_1:0x007faf9fb5b7c8>
     # ./spec/models/answer_spec.rb:12:in `block (3 levels) in <top (required)>'

Any clues as to why this is happening? I should also mention that I'm testing using sqlite3 :memory:.

Upvotes: 2

Views: 1886

Answers (1)

Nick Veys
Nick Veys

Reputation: 23939

The shoulda libraries aren't being included in your simplecov run.

Is simplecov run in a different environment than :test perhaps? That may make the shoulda gem not be loaded if it's only in the :test group in your Gemfile.

How are you including simplecov in your spec_helper? Something like this?

require 'simplecov'
require 'simplecov-rcov'
SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
SimpleCov.start 'rails' if ENV['COVERAGE']

As the very first lines of your spec_helper.rb?

Something like this in your Gemfile?

group :test, :development do
  ...
  gem 'rspec-rails', '~> 2.0'
  gem 'shoulda-matchers'
end

group :test do
  gem 'simplecov'
  gem 'simplecov-rcov'
end

And executing it like so?

$ COVERAGE=true bundle exec rake spec

This recipe is working very well for me in many projects. The RCov formatter may not be important depending on your purposes.

Upvotes: 1

Related Questions