File: proxy_for_nil.rb

package info (click to toggle)
ruby-rspec-mocks 2.14.5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 868 kB
  • ctags: 725
  • sloc: ruby: 8,227; makefile: 4
file content (37 lines) | stat: -rw-r--r-- 1,052 bytes parent folder | download | duplicates (2)
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
module RSpec
  module Mocks
    # @private
    class ProxyForNil < Proxy

      def initialize
        @warn_about_expectations = true
        super nil
      end
      attr_accessor :warn_about_expectations
      alias warn_about_expectations? warn_about_expectations

      def add_message_expectation(location, method_name, opts={}, &block)
        warn(method_name) if warn_about_expectations?
        super
      end

      def add_negative_message_expectation(location, method_name, &implementation)
        warn(method_name) if warn_about_expectations?
        super
      end

      def add_stub(location, method_name, opts={}, &implementation)
        warn(method_name) if warn_about_expectations?
        super
      end

      private

      def warn method_name
        non_rspec_caller = caller.find { |line| !line.include?('lib/rspec/mocks') }
        Kernel.warn("An expectation of :#{method_name} was set on nil. Called from #{non_rspec_caller}. Use allow_message_expectations_on_nil to disable warnings.")
      end

    end
  end
end