mirror of
https://github.com/Fishwaldo/role_model.git
synced 2025-03-15 11:32:04 +00:00
improve and spec custom ArrayIncludingMatcher
This commit is contained in:
parent
b20b9806bc
commit
4a15d83f30
2 changed files with 64 additions and 1 deletions
|
@ -5,8 +5,9 @@ module CustomMatchers
|
|||
end
|
||||
|
||||
def ==(actual)
|
||||
return false if actual.size < @expected.size
|
||||
@expected.each do | value |
|
||||
return false unless actual.include?(value)
|
||||
return false unless actual.any? { |actual_value| value == actual_value }
|
||||
end
|
||||
true
|
||||
rescue NoMethodError => ex
|
||||
|
|
62
spec/custom_matchers_spec.rb
Normal file
62
spec/custom_matchers_spec.rb
Normal file
|
@ -0,0 +1,62 @@
|
|||
require 'spec_helper'
|
||||
|
||||
module CustomMatchers
|
||||
describe ArrayIncludingMatcher do
|
||||
|
||||
it "should describe itself properly" do
|
||||
ArrayIncludingMatcher.new(:a, :b).description.should == "array_including(:a, :b)"
|
||||
end
|
||||
|
||||
describe "passing" do
|
||||
it "should match the same array" do
|
||||
array_including(:a).should == [:a]
|
||||
end
|
||||
|
||||
it "should match a array with extra stuff" do
|
||||
array_including(:a).should == [:a, :b]
|
||||
end
|
||||
|
||||
it "should match a array regardless of element position" do
|
||||
array_including(:a, :b).should == [:b, :a]
|
||||
end
|
||||
|
||||
describe "when matching against other matchers" do
|
||||
it "should match a symbol against anything()" do
|
||||
array_including(anything, :b).should == [:a, :b]
|
||||
end
|
||||
|
||||
it "should match an int against anything()" do
|
||||
array_including(anything, :b).should == [1, :b]
|
||||
end
|
||||
|
||||
it "should match a string against anything()" do
|
||||
array_including(anything, :b).should == ["1", :b]
|
||||
end
|
||||
|
||||
it "should match an arbitrary object against anything()" do
|
||||
array_including(anything, :b).should == [Class.new.new, :b]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "failing" do
|
||||
it "should not match a non-array" do
|
||||
array_including(:a).should_not == :a
|
||||
end
|
||||
|
||||
it "should not match a array with a missing element" do
|
||||
array_including(:a).should_not == [:b]
|
||||
end
|
||||
|
||||
it "should not match an empty array with a given key" do
|
||||
array_including(:a).should_not == []
|
||||
end
|
||||
|
||||
describe "when matching against other matchers" do
|
||||
it "should not match without additional elements" do
|
||||
array_including(anything, 1).should_not == [1]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue