File: syntax_agnostic_message_matchers_spec.rb

package info (click to toggle)
ruby-rspec 3.9.0c2e2m1s3-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,612 kB
  • sloc: ruby: 67,456; sh: 1,572; makefile: 98
file content (96 lines) | stat: -rw-r--r-- 2,888 bytes parent folder | download | duplicates (4)
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
module RSpec
  module Mocks
    RSpec.describe ".allow_message" do
      let(:subject) { Object.new }

      it "sets up basic message allowance" do
        expect {
          ::RSpec::Mocks.allow_message(subject, :basic)
        }.to change {
          subject.respond_to?(:basic)
        }.to(true)

        expect(subject.basic).to eq(nil)
      end

      it "sets up message allowance with params and return value" do
        expect {
          ::RSpec::Mocks.allow_message(subject, :x).with(:in).and_return(:out)
        }.to change {
          subject.respond_to?(:x)
        }.to(true)

        expect(subject.x(:in)).to eq(:out)
      end

      it "supports block implementations" do
        ::RSpec::Mocks.allow_message(subject, :message) { :value }
        expect(subject.message).to eq(:value)
      end

      it "does not set an expectation that the message will be received" do
        ::RSpec::Mocks.allow_message(subject, :message)
        expect { verify subject }.not_to raise_error
      end

      it 'does not get confused when the string and symbol message form are both used' do
        ::RSpec::Mocks.allow_message(subject, :foo).with(1) { :a }
        ::RSpec::Mocks.allow_message(subject, "foo").with(2) { :b }

        expect(subject.foo(1)).to eq(:a)
        expect(subject.foo(2)).to eq(:b)

        reset subject
      end
    end

    RSpec.describe ".expect_message" do
      let(:subject) { Object.new }

      it "sets up basic message expectation, verifies as uncalled" do
        expect {
          ::RSpec::Mocks.expect_message(subject, :basic)
        }.to change {
          subject.respond_to?(:basic)
        }.to(true)

        expect { verify subject }.to fail
      end

      it "fails if never is specified and the message is called" do
        expect_fast_failure_from(subject, /expected.*0 times/) do
          ::RSpec::Mocks.expect_message(subject, :foo).never
          subject.foo
        end
      end

      it "sets up basic message expectation, verifies as called" do
        ::RSpec::Mocks.expect_message(subject, :basic)
        subject.basic
        verify subject
      end

      it "sets up message expectation with params and return value" do
        ::RSpec::Mocks.expect_message(subject, :msg).with(:in).and_return(:out)
        expect(subject.msg(:in)).to eq(:out)
        verify subject
      end

      it "accepts a block implementation for the expected message" do
        ::RSpec::Mocks.expect_message(subject, :msg) { :value }
        expect(subject.msg).to eq(:value)
        verify subject
      end

      it 'does not get confused when the string and symbol message form are both used' do
        ::RSpec::Mocks.expect_message(subject, :foo).with(1)
        ::RSpec::Mocks.expect_message(subject, "foo").with(2)

        subject.foo(1)
        subject.foo(2)

        verify subject
      end
    end
  end
end