File: receivers_test.rb

package info (click to toggle)
ruby-mocha 2.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,540 kB
  • sloc: ruby: 11,899; javascript: 477; makefile: 14
file content (96 lines) | stat: -rw-r--r-- 2,104 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
require File.expand_path('../../test_helper', __FILE__)
require 'mocha/receivers'

class ObjectReceiverTest < Mocha::TestCase
  include Mocha

  class FakeObject
    def initialize(mocha)
      @mocha = mocha
    end

    def mocha(_instantiate)
      @mocha
    end

    def is_a?(_klass)
      false
    end
  end

  class FakeClass
    attr_reader :superclass

    def initialize(superclass, mocha)
      @superclass = superclass
      @mocha = mocha
    end

    def mocha(_instantiate)
      @mocha
    end

    def is_a?(klass)
      klass == Class
    end
  end

  def test_mocks_returns_mock_for_object
    object = FakeObject.new(:mocha)
    receiver = ObjectReceiver.new(object)
    assert_equal [:mocha], receiver.mocks
  end

  def test_mocks_returns_mocks_for_class_and_its_superclasses
    grandparent = FakeClass.new(nil, :grandparent_mocha)
    parent = FakeClass.new(grandparent, :parent_mocha)
    klass = FakeClass.new(parent, :mocha)
    receiver = ObjectReceiver.new(klass)
    assert_equal [:mocha, :parent_mocha, :grandparent_mocha], receiver.mocks
  end
end

class AnyInstanceReceiverTest < Mocha::TestCase
  include Mocha

  class FakeAnyInstanceClass
    class AnyInstance
      def initialize(mocha)
        @mocha = mocha
      end

      def mocha(_instantiate)
        @mocha
      end
    end

    attr_reader :superclass

    def initialize(superclass, mocha)
      @superclass = superclass
      @mocha = mocha
    end

    def any_instance
      AnyInstance.new(@mocha)
    end
  end

  def test_mocks_returns_mocks_for_class_and_its_superclasses
    grandparent = FakeAnyInstanceClass.new(nil, :grandparent_mocha)
    parent = FakeAnyInstanceClass.new(grandparent, :parent_mocha)
    klass = FakeAnyInstanceClass.new(parent, :mocha)
    receiver = AnyInstanceReceiver.new(klass)
    assert_equal [:mocha, :parent_mocha, :grandparent_mocha], receiver.mocks
  end
end

class DefaultReceiverTest < Mocha::TestCase
  include Mocha

  def test_mocks_returns_mock
    mock = :mocha
    receiver = DefaultReceiver.new(mock)
    assert_equal [:mocha], receiver.mocks
  end
end