Reputation: 329
'shoulda-matchers' gem is not loading when running rspec tests.
Error:
NoMethodError: undefined method 'allow_value' for #<RSpec::ExampleGroups::Contact::ActiveModelValidationss:0x007fef1021b310>
versions being used:
Ruby 2.2.2
Rails 4.2.1
shoulda-matchers 3.1.1
Gemfile:
source 'https://rubygems.org'
gem 'rails', '4.2.1'
gem 'sqlite3'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
group :development, :test do
gem 'byebug'
gem 'rspec-rails', '~> 3.0'
gem 'factory_girl_rails'
gem 'shoulda-matchers', '~> 3.1'
gem 'web-console', '~> 2.0'
gem 'spring'
gem 'rubocop', require: false
end
rails_helper.rb:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :active_record
with.library :rails
end
end
end
spec_helper.rb:
RSpec.configure do |config|
require 'factory_girl_rails'
require 'shoulda/matchers'
config.include FactoryGirl::Syntax::Methods
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
contact_spec.rb:
require 'spec_helper'
describe Contact do
FactoryGirl.define do
factory :contact do
first_name "Alice"
last_name "Bob"
email_address "[email protected]"
phone_number "555-555-5555"
company_name "ExampleCompany"
end
end
it "has a valid factory" do
expect(build(:contact)).to be_valid
end
describe "ActiveModel validations" do
it { expect(:contact).to allow_value("[email protected]").for(:email_address) }
end
end
Sorry for all the code. Please note I am using shoulda-matchers version 3.1.1 and adding 'require: false` to the gemfile is no longer needed as noted here https://robots.thoughtbot.com/shoulda-matchers-3-0
I have also tried adding shoulda-matchers to its own test environment in the Gemfile as below but found no change
group :test do
gem 'shoulda-matchers', '~> 3.1'
end
I've looked online and have found similar problems with solutions that don't work in my case. Any help or advice would be great!
Upvotes: 0
Views: 1436
Reputation: 499
In my case, the problem was that the tested class was not an ActiveRecord
item, so shoulda
did not load methods such as allow_value
by default.
Solution was to add type: :model
in the _spec.rb
file:
RSpec.describe MyTestedClass, type: :model do
...
end
Upvotes: 2
Reputation: 16793
The fix that should get you green again is to change:
it { expect(:contact).to allow_value("[email protected]").for(:email_address) }
to
it { expect(build(:contact)).to allow_value("[email protected]").for(:email_address) }
in the same way as your first test, as your second test is expecting the symbol :contact
to allow_value
, rather than the object created in your factory.
Further, I'd recommend moving your factory definition into its own file under spec/factories/contact.rb
rather than inline in your spec, and memoise the contact object in a let
method, so you'd have a spec that looks like:
require 'spec_helper'
describe Contact do
let(:contact) { build(:contact) }
it "has a valid factory" do
expect(contact).to be_valid
end
describe "ActiveModel validations" do
it { expect(contact).to allow_value("[email protected]").for(:email_address) }
end
end
Upvotes: 0