Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shoulda-matchers are not seen by rails/rspec

Rails 4.2.4, Rspec 3.3.1, shoulda-matchers 3.0.0.

I am getting

  #...
  358) Participant validations 
       Failure/Error: it { should ensure_length_of(:coresp_country).is_at_most(255) }
       NoMethodError:
         undefined method `ensure_length_of' for #<RSpec::ExampleGroups::Participant::Validations:0x0000000f40aec0>
       # ./spec/models/participant_spec.rb:100:in `block (3 levels) in <top (required)>'

  359) Participant validations company 
       Failure/Error: it { should ensure_length_of(:company).is_at_most(255) }
       NoMethodError:
         undefined method `ensure_length_of' for #<RSpec::ExampleGroups::Participant::Validations::Company:0x0000000f414ab0>
       # ./spec/models/participant_spec.rb:149:in `block (4 levels) in <top (required)>'

  360) Participant validations company declared_type = COMPANY 
       Failure/Error: it { should validate_presence_of(:company) }
       NoMethodError:
         undefined method `validate_presence_of' for #<RSpec::ExampleGroups::Participant::Validations::Company::DeclaredTypeCOMPANY:0x0000000f429c58>
   #...

And many more failures of this kind (looks like shoulda-matchers do not work).

rails_helper.rb:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'spec_helper'
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

Rails.logger.level = 4

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!

  config.include FactoryGirl::Syntax::Methods
  config.include Sorcery::TestHelpers::Rails
  config.include Macros::Controller::Security
end
FactoryGirl.reload
Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

spec_helper.rb:

require 'simplecov_helper'
require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true)
require 'rspec/collection_matchers'

RSpec.configure do |config|
  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
  config.filter_run :focus
  config.run_all_when_everything_filtered = true
  config.disable_monkey_patching!
  config.expose_dsl_globally = true
  config.default_formatter = 'doc' if config.files_to_run.one?
  config.order = :random

  Kernel.srand config.seed
end

EDIT

Ok, I think the issue is not with shoulda-matchers, but with active_attr gem, because tests only fails in spec/compositions/api folder, where I use the gem.

like image 537
Andrey Deineko Avatar asked Jan 07 '23 07:01

Andrey Deineko


1 Answers

shoulda-matchers 3.0 selectively makes its matchers available. I am using ActiveModel matchers which are only mixed into model specs (af98a23).

I had two options to solve the issue:

  • placing the specs of the models, which use active_attr gem under spec/models folder;
  • adding type: :model to the top-level describe block for each class in spec/compositions/api folder.

I decided to go with second option and it worked.


Sidenote

Now matchers, which starts with ensure (ensure_inclusion_in, ensure_length_of) are renamed to validate_inclusion_in, validate_length_of (55c8d09).

like image 148
Andrey Deineko Avatar answered Jan 18 '23 13:01

Andrey Deineko