Drew
Drew

Reputation: 2663

Shoulda-matchers not working for PORO

So I have the following spec:

require 'rails_helper'

class SpecRecord
  # include ActiveModel::Model
  include ActiveModel::Validations
  attr_accessor :email
  validates :email, rfc_compliant: true
end

describe RfcCompliantValidator do
  subject { SpecRecord.new }

   it { is_expected.to allow_value('[email protected]').for(:email) }
   it { is_expected.to allow_value('[email protected]').for(:email) }
   it { is_expected.to allow_value('[email protected]').for(:email) }
   it { is_expected.to allow_value('[email protected]').for(:email) }
   it { is_expected.to allow_value('[email protected]').for(:email) }
   it { is_expected.to allow_value("0123456789#!$%&'*+-/=?^_`{}|[email protected]").for(:email) }

   it { is_expected.to_not allow_value('john316').for(:email) }
   it { is_expected.to_not allow_value('[email protected]').for(:email) }
   it { is_expected.to_not allow_value('this\ still\"not\\[email protected]').for(:email) }
   it { is_expected.to_not allow_value('test@ex:amp,le.com').for(:email) }
   it { is_expected.to_not allow_value('t:[email protected]').for(:email) }
   it { is_expected.to_not allow_value('te,[email protected]').for(:email) }
   it { is_expected.to_not allow_value('te()[email protected]').for(:email) }
   it { is_expected.to_not allow_value('te[][email protected]').for(:email) }
   it { is_expected.to_not allow_value('te<>[email protected]').for(:email) }
   it { is_expected.to_not allow_value('te"[email protected]').for(:email) }
   it { is_expected.to_not allow_value("test@exampledotcom").for(:email) }
   it { is_expected.to_not allow_value("testexample").for(:email) }
   it { is_expected.to_not allow_value("[email protected]").for(:email) }
   it { is_expected.to_not allow_value("78901234567890123456789012345678901234567890123456789012345678901212+x@example.com").for(:email) }
 end

In rails_helper I have the following defined:

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    # Choose a test framework:
    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

In my other tests that leverage allow_value, the tests seem to be working, but for some reason allow_value isn't found here. Any thoughts?

Upvotes: 1

Views: 234

Answers (1)

engineersmnky
engineersmnky

Reputation: 29328

allow_value is part of the Shoulda::Matchers::ActiveModel module for scoping purposes.

Whether or not this module is including in the tested scope is determined by whether or not the object being tested is marked as a :model object via the option type passed to the describe block.Source and Source for RSpec configuration

since your spec does not mark RfcCompliantValidator as a :model these methods are not included in the testing scope.

To resolve this all you need to do is mark it as such

describe RfcCompliantValidator, type: :model do 
   ###

and the AllowValueMatcher will be available in your tests.

Upvotes: 2

Related Questions