File: stub_instance_method_defined_on_kernel_module_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 (136 lines) | stat: -rw-r--r-- 4,378 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
require File.expand_path('../acceptance_test_helper', __FILE__)

class StubInstanceMethodDefinedOnKernelModuleTest < Mocha::TestCase
  include AcceptanceTest

  def setup
    setup_acceptance_test
  end

  def teardown
    teardown_acceptance_test
  end

  def test_should_stub_public_method_and_leave_it_unchanged_after_test
    Kernel.module_eval do
      def my_instance_method
        :original_return_value
      end
      public :my_instance_method
    end
    instance = Class.new.new
    assert_snapshot_unchanged(instance) do
      test_result = run_as_test do
        instance.stubs(:my_instance_method).returns(:new_return_value)
        assert_equal :new_return_value, instance.my_instance_method
      end
      assert_passed(test_result)
    end
    assert_equal :original_return_value, instance.my_instance_method
  ensure
    Kernel.module_eval { remove_method :my_instance_method }
  end

  def test_should_stub_protected_method_and_leave_it_unchanged_after_test
    Kernel.module_eval do
      def my_instance_method
        :original_return_value
      end
      protected :my_instance_method
    end
    instance = Class.new.new
    assert_snapshot_unchanged(instance) do
      test_result = run_as_test do
        instance.stubs(:my_instance_method).returns(:new_return_value)
        assert_equal :new_return_value, instance.send(:my_instance_method)
      end
      assert_passed(test_result)
    end
    assert_equal :original_return_value, instance.send(:my_instance_method)
  ensure
    Kernel.module_eval { remove_method :my_instance_method }
  end

  def test_should_stub_private_method_and_leave_it_unchanged_after_test
    Kernel.module_eval do
      def my_instance_method
        :original_return_value
      end
      private :my_instance_method
    end
    instance = Class.new.new
    assert_snapshot_unchanged(instance) do
      test_result = run_as_test do
        instance.stubs(:my_instance_method).returns(:new_return_value)
        assert_equal :new_return_value, instance.send(:my_instance_method)
      end
      assert_passed(test_result)
    end
    assert_equal :original_return_value, instance.send(:my_instance_method)
  ensure
    Kernel.module_eval { remove_method :my_instance_method }
  end

  def test_should_stub_public_module_method_and_leave_it_unchanged_after_test
    Kernel.module_eval do
      def my_instance_method
        :original_return_value
      end
      public :my_instance_method
    end
    mod = Module.new
    assert_snapshot_unchanged(mod) do
      test_result = run_as_test do
        mod.stubs(:my_instance_method).returns(:new_return_value)
        assert_method_visibility mod, :my_instance_method, :public
        assert_equal :new_return_value, mod.my_instance_method
      end
      assert_passed(test_result)
    end
    assert_equal :original_return_value, mod.my_instance_method
  ensure
    Kernel.module_eval { remove_method :my_instance_method }
  end

  def test_should_stub_protected_module_method_and_leave_it_unchanged_after_test
    Kernel.module_eval do
      def my_instance_method
        :original_return_value
      end
      protected :my_instance_method
    end
    mod = Module.new
    assert_snapshot_unchanged(mod) do
      test_result = run_as_test do
        mod.stubs(:my_instance_method).returns(:new_return_value)
        assert_method_visibility mod, :my_instance_method, :protected
        assert_equal :new_return_value, mod.send(:my_instance_method)
      end
      assert_passed(test_result)
    end
    assert_equal :original_return_value, mod.send(:my_instance_method)
  ensure
    Kernel.module_eval { remove_method :my_instance_method }
  end

  def test_should_stub_private_module_method_and_leave_it_unchanged_after_test
    Kernel.module_eval do
      def my_instance_method
        :original_return_value
      end
      private :my_instance_method
    end
    mod = Module.new
    assert_snapshot_unchanged(mod) do
      test_result = run_as_test do
        mod.stubs(:my_instance_method).returns(:new_return_value)
        assert_method_visibility mod, :my_instance_method, :private
        assert_equal :new_return_value, mod.send(:my_instance_method)
      end
      assert_passed(test_result)
    end
    assert_equal :original_return_value, mod.send(:my_instance_method)
  ensure
    Kernel.module_eval { remove_method :my_instance_method }
  end
end