2

I am learning TDD and trying to use shoulda_matchers to help with my testing but I get a very strange error. Here is my test:

spec/models/idea_spec.rb

require 'rails_helper'

describe Idea do

  context 'Validations' do
    describe 'title' do
       it { should validate_presence_of(:title) }
    end
  end
end

The error on the test says:

Idea Validations title 
 Failure/Error: it { should validate_presence_of(:title) }
 NoMethodError:
   undefined method `validate_presence_of' for #<RSpec::ExampleGroups::Idea_3::Validations::Title:0x007f056f9fcdf8>
 # ./spec/models/idea_spec.rb:7:in `block (4 levels) in <top (required)>'

require 'shoulda/matchers' is at the top of my rails_helper file as per the gem instructions.

Gems I'm using:

group :development, :test do
  gem 'spring'
  gem 'dotenv-rails'
  gem 'rspec-rails', '~> 3.0'
  gem 'factory_girl_rails'
end

group :test do
  gem 'webmock', '~> 1.20.4'
  gem 'shoulda-matchers', require: false
end
  1. ruby '2.1.2'
  2. rails', '4.1.9'
Makoto
  • 104,088
  • 27
  • 192
  • 230
MandyM
  • 87
  • 3
  • 11

7 Answers7

12

Shoulda-Matchers no longer installs itself into your test framework automatically. You need to add this to your rails_helper:

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

See this GitHub issue, read the note on shoulda-matchers configuration, and read this blog post by maintainers.

Makoto
  • 104,088
  • 27
  • 192
  • 230
Uzzar
  • 705
  • 1
  • 12
  • 24
5

That require: false in your Gemfile means the matchers aren't being loaded when you run your tests. In rails_helper.rb you need to add the line require 'shoulda/matchers' at the top.

tpbowden
  • 2,040
  • 15
  • 22
2

If the platform for some reason mandates that you make the shoulda-matchers dependency optional, then you will have to add require 'shoulda/matchers' at the top of every test you write.

If there is no mandate, however, then elect to remove require: false from the Gemspec instead.

Makoto
  • 104,088
  • 27
  • 192
  • 230
0

I had the same problem and fixed it with those following tips :

  • in your spec :

    require 'rails_helper'
    
  • /rails_helper

    require 'rspec/rails'
    require 'shoulda/matchers'
    
  • Still in /rails_helpers, do not add :

    Shoulda::Matchers.configure do |config|
      config.integrate do |with|
        with.test_framework :rspec
        with.library :rails
      end
    end
    
  • /gemfile

     group :development, :test do
       gem 'shoulda-matchers', '~> 2.5.0', require: false
    
  • Do not require the gem in spec_helper

My version are different (Rails 4.2.3 and Ruby 2.3.0) but you can still test and try to adapt

Orsay
  • 1,080
  • 2
  • 12
  • 32
0

spec_helper.rb

  config.include(Shoulda::Matchers::ActiveRecord, type: :model)

rails_helper.rb

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end
Sukeerthi Adiga
  • 531
  • 5
  • 9
0

following the latest updated : http://matchers.shoulda.io/docs/v3.1.1/

in ur Gemfile
group :test do
  gem 'shoulda-matchers', '~> 3.0' 
end
and in the end of rails_helpers.rb
Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    # Choose a test framework:
    #with.test_framework :minitest
    #with.test_framework :minitest_4
    #with.test_framework :test_unit
    with.test_framework :rspec

    # Choose one or more libraries:
    with.library :active_record
    with.library :active_model
    with.library :action_controller
    # Or, choose the following (which implies all of the above):
    with.library :rails
  end
end
Samda
  • 1,439
  • 12
  • 5
-2

After:

context 'Validations' do

add

before { FactoryGirl.build(:idea) }
Undo
  • 25,519
  • 37
  • 106
  • 129
BGuimberteau
  • 249
  • 1
  • 8