thoughtbot--shoulda-matchers/spec/unit_spec_helper.rb

55 lines
1.9 KiB
Ruby
Raw Normal View History

require_relative 'support/unit/load_environment'
2012-03-24 01:00:36 +00:00
require 'rspec/rails'
Fix inclusion to correctly disallow outside values The inclusion matcher, when qualified with `in_array`, was using AllowValueMatcher to check that values outside the array were disallowed by the model (and then inverting its result). However, it should have been using DisallowValueMatcher all this time. This commit fixes that. Without this fix, the following error is raised when using the inclusion matcher against a model which does not have the proper inclusion validation on it: undefined method `attribute_setter' for nil:NilClass This happens because the inclusion matcher is a complex matcher, i.e., it runs a series of submatchers internally and the result of those submatchers contributes to whether or not the matcher matches. If one of those submatchers fails, the inclusion matcher immediately fails and spits out the failure message associated with that submatcher. However, there is a fundamental difference between AllowValueMatcher and DisallowValueMatcher as it relates to how they function: * AllowValueMatcher sets an attribute to a value on a record and expects the record not to fail validation. * DisallowValueMatcher sets an attribute to a value on a record, but expects the record *to* fail validation. The issue in this case is that, because AllowValueMatcher was used instead of DisallowValueMatcher, the inclusion matcher thought that the AllowValueMatcher failed, when in fact it passed (this result was just inverted). So it tried to generate a failure message for a matcher that didn't fail in the first place. By using DisallowValueMatcher, we set the proper expectations.
2018-09-12 05:05:06 +00:00
require 'rspec/matchers/fail_matchers'
require 'shoulda-matchers'
require 'spec_helper'
Dir[ File.join(File.expand_path('../support/unit/**/*.rb', __FILE__)) ].sort.each do |file|
require file
end
RSpec.configure do |config|
Fix inclusion to correctly disallow outside values The inclusion matcher, when qualified with `in_array`, was using AllowValueMatcher to check that values outside the array were disallowed by the model (and then inverting its result). However, it should have been using DisallowValueMatcher all this time. This commit fixes that. Without this fix, the following error is raised when using the inclusion matcher against a model which does not have the proper inclusion validation on it: undefined method `attribute_setter' for nil:NilClass This happens because the inclusion matcher is a complex matcher, i.e., it runs a series of submatchers internally and the result of those submatchers contributes to whether or not the matcher matches. If one of those submatchers fails, the inclusion matcher immediately fails and spits out the failure message associated with that submatcher. However, there is a fundamental difference between AllowValueMatcher and DisallowValueMatcher as it relates to how they function: * AllowValueMatcher sets an attribute to a value on a record and expects the record not to fail validation. * DisallowValueMatcher sets an attribute to a value on a record, but expects the record *to* fail validation. The issue in this case is that, because AllowValueMatcher was used instead of DisallowValueMatcher, the inclusion matcher thought that the AllowValueMatcher failed, when in fact it passed (this result was just inverted). So it tried to generate a failure message for a matcher that didn't fail in the first place. By using DisallowValueMatcher, we set the proper expectations.
2018-09-12 05:05:06 +00:00
config.include RSpec::Matchers::FailMatchers
UnitTests::ActionPackVersions.configure_example_group(config)
UnitTests::ActiveModelHelpers.configure_example_group(config)
UnitTests::ActiveModelVersions.configure_example_group(config)
UnitTests::ClassBuilder.configure_example_group(config)
UnitTests::ControllerBuilder.configure_example_group(config)
UnitTests::I18nFaker.configure_example_group(config)
UnitTests::MailerBuilder.configure_example_group(config)
UnitTests::ModelBuilder.configure_example_group(config)
UnitTests::RailsVersions.configure_example_group(config)
UnitTests::ActiveRecordVersions.configure_example_group(config)
UnitTests::ActiveModelVersions.configure_example_group(config)
UnitTests::DatabaseHelpers.configure_example_group(config)
UnitTests::ColumnTypeHelpers.configure_example_group(config)
UnitTests::ValidationMatcherScenarioHelpers.configure_example_group(config)
UnitTests::MessageHelpers.configure_example_group(config)
if UnitTests::RailsVersions.rails_lte_4?
UnitTests::ActiveResourceBuilder.configure_example_group(config)
end
config.include UnitTests::Matchers
config.infer_spec_type_from_file_location!
config.example_status_persistence_file_path = "spec/examples.txt"
config.alias_it_behaves_like_to(:it_supports, "it supports")
config.before(:all, type: :controller) do
self.class.controller(ApplicationController) { }
end
end
ActiveSupport::Deprecation.behavior = :stderr
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end