Use an arbitrary string for ensure_inclusion instead of using next function
This commit is contained in:
parent
a228e0e0ef
commit
6f6b55a5aa
|
@ -22,6 +22,8 @@ module Shoulda # :nodoc:
|
|||
end
|
||||
|
||||
class EnsureInclusionOfMatcher < ValidationMatcher # :nodoc:
|
||||
ARBITRARY_OUTSIDE_STRING = "shouldamatchersteststring"
|
||||
|
||||
def initialize(attribute)
|
||||
super(attribute)
|
||||
@options = {}
|
||||
|
@ -138,20 +140,14 @@ module Shoulda # :nodoc:
|
|||
end
|
||||
|
||||
def disallows_value_outside_of_array?
|
||||
if value_outside_of_array
|
||||
disallows_value_of(value_outside_of_array)
|
||||
else
|
||||
raise CouldNotDetermineValueOutsideOfArray
|
||||
end
|
||||
disallows_value_of(value_outside_of_array)
|
||||
end
|
||||
|
||||
def value_outside_of_array
|
||||
found = @array.detect do |item|
|
||||
!@array.include?(item.next)
|
||||
end
|
||||
|
||||
if found
|
||||
found.next
|
||||
if @array.include?(ARBITRARY_OUTSIDE_STRING)
|
||||
raise CouldNotDetermineValueOutsideOfArray
|
||||
else
|
||||
ARBITRARY_OUTSIDE_STRING
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -12,11 +12,20 @@ describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do
|
|||
end
|
||||
end
|
||||
|
||||
context "with true/false values" do
|
||||
it "can verify outside values to ensure the negative case" do
|
||||
model = define_model(:example, :attr => :string).new
|
||||
|
||||
model.should_not ensure_inclusion_of(:attr).in_array([true, false])
|
||||
end
|
||||
end
|
||||
|
||||
context "where we cannot determine a value outside the array" do
|
||||
it "should raise a custom exception" do
|
||||
@model = define_model(:example, :attr => :string).new
|
||||
model = define_model(:example, :attr => :string).new
|
||||
|
||||
expect { @model.should ensure_inclusion_of(:attr).in_array([""]) }.to raise_error Shoulda::Matchers::ActiveModel::CouldNotDetermineValueOutsideOfArray
|
||||
arbitrary_string = Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher::ARBITRARY_OUTSIDE_STRING
|
||||
expect { model.should ensure_inclusion_of(:attr).in_array([arbitrary_string]) }.to raise_error Shoulda::Matchers::ActiveModel::CouldNotDetermineValueOutsideOfArray
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue