File: partial_mock_spec.rb

package info (click to toggle)
ruby-spy 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 360 kB
  • sloc: ruby: 3,101; makefile: 2
file content (81 lines) | stat: -rw-r--r-- 2,038 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
require 'spec_helper'

module Spy
  describe "using a Partial Mock," do

    def stub(object, method_name)
      Spy::Subroutine.new(object, method_name).hook(force: true)
    end

    let(:object) { Object.new }

    it "names the class in the failure message" do
      spy = stub(object, :foo)
      expect(spy).to_not have_been_called
    end

    it "names the class in the failure message when expectation is on class" do
      spy = stub(Object, :foo)
      expect(spy).to_not have_been_called
    end

    it "does not conflict with @options in the object" do
      object.instance_eval { @options = Object.new }
      spy = stub(object, :blah)
      object.blah
      expect(spy).to have_been_called
    end

    it "uses reports nil in the error message" do
      allow_message_expectations_on_nil

      _nil = nil
      spy = stub(_nil, :foobar)
      _nil.foobar
      expect(spy).to have_been_called
    end
  end

  describe "Method visibility when using partial mocks" do
    def stub(o, method_name)
      Spy.on(o, method_name)
    end

    let(:klass) do
      Class.new do
        def public_method
          private_method
          protected_method
        end
        protected
        def protected_method; end
        private
        def private_method; end
      end
    end

    let(:object) { klass.new }

    it 'keeps public methods public' do
      spy = stub(object, :public_method)
      expect(object.public_methods).to include_method(:public_method)
      object.public_method
      expect(spy).to have_been_called
    end

    it 'keeps private methods private' do
      spy = stub(object, :private_method)
      expect(object.private_methods).to include_method(:private_method)
      object.public_method
      expect(spy).to have_been_called
    end

    it 'keeps protected methods protected' do
      spy = stub(object, :protected_method)
      expect(object.protected_methods).to include_method(:protected_method)
      object.public_method
      expect(spy).to have_been_called
    end

  end
end