File: syntax_agnostic_message_matchers_spec.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 (81 lines) | stat: -rw-r--r-- 2,293 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 RSpec
  module Mocks

    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
    end

    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 raise_error(RSpec::Mocks::MockExpectationError)
      end

      it "fails if never is specified and the message is called" do
        expect {
          ::RSpec::Mocks.expect_message(subject, :foo).never
          subject.foo
        }.to raise_error(/expected.*0 times/)
      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

    end

  end
end