shoulda-matchers: undefined method `attribute_setter' for nil:NilClass
Hello!
I’m receiving an error when a validation fails with the validates_inclusion_of
matcher. If the validation is present, everything is fine, but without it when building the failure message you get this error:
NoMethodError:
undefined method `attribute_setter' for nil:NilClass
# ./.gem/ruby/2.3.0/gems/shoulda-matchers-3.1.1/lib/shoulda/matchers/active_model/allow_value_matcher.rb:405:in `failure_message'
# ./.gem/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/core_ext/object/try.rb:77:in `public_send'
# ./.gem/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/core_ext/object/try.rb:77:in `try!'
# ./.gem/ruby/2.3.0/gems/activesupport-4.2.5.1/lib/active_support/core_ext/object/try.rb:63:in `try'
# ./.gem/ruby/2.3.0/gems/shoulda-matchers-3.1.1/lib/shoulda/matchers/active_model/validation_matcher.rb:129:in `failure_reason'
# ./.gem/ruby/2.3.0/gems/shoulda-matchers-3.1.1/lib/shoulda/matchers/active_model/validation_matcher.rb:56:in `block in failure_message'
# ./.gem/ruby/2.3.0/gems/shoulda-matchers-3.1.1/lib/shoulda/matchers/active_model/validation_matcher.rb:55:in `tap'
# ./.gem/ruby/2.3.0/gems/shoulda-matchers-3.1.1/lib/shoulda/matchers/active_model/validation_matcher.rb:55:in `failure_message'
# ./spec/lib/models/address_spec.rb:22:in `block (2 levels) in <module:Models>'
My class and spec look close to this:
class Thing
include Virtus.model
include ActiveModel::Model
ARRAY = ["value"]
attribute :thing, String
end
RSpec.describe Thing do
it { is_expected.to validate_inclusion_of(:thing).in_array(Thing::ARRAY) }
end
I am using active model outside of rails, configuring Shoulda
to integrate with RSpec
and ActiveModel
. Ruby version is 2.3.0 and ActiveModel version is 4.2.5.1
Maybe this is an incompatibility with Virtus that I’m not aware of?
Thanks
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 17
- Comments: 27
This should be fixed in master now. We’re cooking up an RC so that y’all can try out the fix. We’ll post again when we have that.
What’s the status on this one? Has anyone come up with a solution?
it can be workarounded with
allow_value
matcher tho:This error will also occur when testing that a presence validation does not exist when it actually does:
@wilianto
That’s not a bad idea, but we would probably have to make the same change across the board, so I would hesitate to do that.
I’m (slowly) working on a branch that will change how “submatchers” (those are matchers that are used internally by other matches) are processed and should address this issue here and anywhere else it appears.
I just got this error as well when saying
should_not validate_confirmation_of(:some_attribute)
. It doesn’t matter if the test actually passes or not, it will still crash.