Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added more helpful message when actual has #[] but does not support #…

…[0,3]

Pull request #135
  • Loading branch information...
commit 59081072f560937e1b16bb2415c5027025ba8f98 1 parent 35d98aa
@jeremywadsack jeremywadsack authored dchelimsky committed
View
6 lib/rspec/matchers/built_in/start_and_end_with.rb
@@ -10,7 +10,11 @@ def initialize(*expected)
def matches?(actual)
@actual = actual.respond_to?(:[]) ? actual : (raise ArgumentError.new("#{actual.inspect} does not respond to :[]"))
- @expected.respond_to?(:length) ? subset_matches?(@expected, @actual) : element_matches?(@expected, @actual)
+ begin
+ @expected.respond_to?(:length) ? subset_matches?(@expected, @actual) : element_matches?(@expected, @actual)
+ rescue ArgumentError
+ raise ArgumentError.new("#{actual.inspect} does not have ordered elements")
+ end
end
def failure_message_for_should
View
17 spec/rspec/matchers/start_with_end_with_spec.rb
@@ -40,6 +40,14 @@
expect { Object.new.should start_with 0 }.to raise_error(ArgumentError, /does not respond to :\[\]/)
end
end
+
+ context "with a hash" do
+ it "raises an ArgumentError if trying to match more than one element" do
+ expect{
+ {a: 'b', b: 'b', c: 'c'}.should start_with({a: 'b', b: 'b'})
+ }.to raise_error(ArgumentError, /does not have ordered elements/)
+ end
+ end
end
describe "should_not start_with" do
@@ -118,6 +126,15 @@
expect { Object.new.should end_with 0 }.to raise_error(ArgumentError, /does not respond to :\[\]/)
end
end
+
+ context "with a hash" do
+ it "raises an ArgumentError if trying to match more than one element" do
+ expect{
+ {a: 'b', b: 'b', c: 'c'}.should end_with({a: 'b', b: 'b'})
+ }.to raise_error(ArgumentError, /does not have ordered elements/)
+ end
+ end
+
end
describe "should_not end_with" do
Please sign in to comment.
Something went wrong with that request. Please try again.