File: rspec_spy_matcher.rb

package info (click to toggle)
ruby-flexmock 3.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 836 kB
  • sloc: ruby: 7,572; makefile: 6
file content (104 lines) | stat: -rw-r--r-- 1,944 bytes parent folder | download | duplicates (3)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
require 'flexmock/spy_describers'

class FlexMock
  module RSpecMatchers

    class HaveReceived
      include SpyDescribers

      def initialize(method_name)
        @method_name = method_name
        @args = nil
        @block = nil
        @times = nil
        @needs_block = nil
        @additional_validations = []
      end

      def matches?(spy)
        @spy = spy
        @options = construct_options
        @spy.flexmock_received?(@method_name, @args, @options)
      end

      def failure_message_for_should
        failure_message
      end

      def failure_message
        describe_spy_expectation(@spy, @method_name, @args, @options)
      end

      def failure_message_for_should_not
        failure_message_when_negated
      end

      def failure_message_when_negated
        describe_spy_negative_expectation(@spy, @method_name, @args, @options)
      end

      def description
        spy_description(@spy, @method_name, @args, @options)
      end

      def with(*args)
        @args = args
        self
      end

      def with_a_block
        @needs_block = true
        self
      end

      def without_a_block
        @needs_block = false
        self
      end

      def times(n)
        @times = n
        self
      end

      def never
        times(0)
      end

      def once
        times(1)
      end

      def twice
        times(2)
      end

      def on(on_count)
        @on_count = on_count
        self
      end

      def and(&block)
        @additional_validations << block
        self
      end

      def construct_options
        {
          :times      => @times,
          :with_block => @needs_block,
          :on_count   => @on_count,
          :and        => @additional_validations,
        }
      end
    end

    def have_received(method_name)
      HaveReceived.new(method_name)
    end
  end
end

RSpec::configure do |config|
  config.include(FlexMock::RSpecMatchers)
end