trotter (owner)

Revisions

gist: 62943 Download_button fork
public
Public Clone URL: git://gist.github.com/62943.git
Embed All Files: show embed
spec_helper.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module Spec
  module Mocks
    module ArgumentMatchers
 
      class ArrayIncludingMatcher
        # We'll allow an array of arguments to be passed in, so that you can do
        # things like obj.should_receive(:blah).with(array_including('a', 'b'))
        def initialize(*expected)
          @expected = expected
        end
 
        # actual is the array (hopefully) passed to the method by the user.
        # We'll check that it includes all the expected values, and return false
        # if it doesn't or if we blow up because #include? is not defined.
        def ==(actual)
          @expected.each do |expected|
            return false unless actual.include?(expected)
          end
          true
        rescue NoMethodError => ex
          return false
        end
 
        def description
          "array_including(#{@expected.join(', ')})"
        end
      end
 
      class ArrayNotIncludingMatcher
        def initialize(*expected)
          @expected = expected
        end
 
        def ==(actual)
          @expected.each do |expected|
            return false if actual.include?(expected)
          end
          true
        rescue NoMethodError => ex
          return false
        end
 
        def description
          "array_not_including(#{@expected.join(', ')})"
        end
      end
 
      # array_including is a helpful wrapper that allows us to actually type
      # #with(array_including(...)) instead of ArrayIncludingMatcher.new(...)
      def array_including(*args)
        ArrayIncludingMatcher.new(*args)
      end
 
      def array_not_including(*args)
        ArrayNotIncludingMatcher.new(*args)
      end
 
    end
  end
end