File: failing_argument_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 (110 lines) | stat: -rw-r--r-- 4,419 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
require 'spec_helper'

module RSpec
  module Mocks
    describe "failing MockArgumentMatchers" do
      before(:each) do
        @double = double("double")
        @reporter = double("reporter").as_null_object
      end

      after(:each) do
        reset @double
      end

      it "rejects non boolean" do
        @double.should_receive(:random_call).with(boolean())
        expect do
          @double.random_call("false")
        end.to raise_error(RSpec::Mocks::MockExpectationError)
      end

      it "rejects non numeric" do
        @double.should_receive(:random_call).with(an_instance_of(Numeric))
        expect do
          @double.random_call("1")
        end.to raise_error(RSpec::Mocks::MockExpectationError)
      end

      it "rejects non string" do
        @double.should_receive(:random_call).with(an_instance_of(String))
        expect do
          @double.random_call(123)
        end.to raise_error(RSpec::Mocks::MockExpectationError)
      end

      it "rejects goose when expecting a duck" do
        @double.should_receive(:random_call).with(duck_type(:abs, :div))
        expect { @double.random_call("I don't respond to :abs or :div") }.to raise_error(RSpec::Mocks::MockExpectationError)
      end

      it "fails if regexp does not match submitted string" do
        @double.should_receive(:random_call).with(/bcd/)
        expect { @double.random_call("abc") }.to raise_error(RSpec::Mocks::MockExpectationError)
      end

      it "fails if regexp does not match submitted regexp" do
        @double.should_receive(:random_call).with(/bcd/)
        expect { @double.random_call(/bcde/) }.to raise_error(RSpec::Mocks::MockExpectationError)
      end

      it "fails for a hash w/ wrong values" do
        @double.should_receive(:random_call).with(:a => "b", :c => "d")
        expect do
          @double.random_call(:a => "b", :c => "e")
        end.to raise_error(RSpec::Mocks::MockExpectationError, /Double "double" received :random_call with unexpected arguments\n  expected: \(\{(:a=>\"b\", :c=>\"d\"|:c=>\"d\", :a=>\"b\")\}\)\n       got: \(\{(:a=>\"b\", :c=>\"e\"|:c=>\"e\", :a=>\"b\")\}\)/)
      end

      it "fails for a hash w/ wrong keys" do
        @double.should_receive(:random_call).with(:a => "b", :c => "d")
        expect do
          @double.random_call("a" => "b", "c" => "d")
        end.to raise_error(RSpec::Mocks::MockExpectationError, /Double "double" received :random_call with unexpected arguments\n  expected: \(\{(:a=>\"b\", :c=>\"d\"|:c=>\"d\", :a=>\"b\")\}\)\n       got: \(\{(\"a\"=>\"b\", \"c\"=>\"d\"|\"c\"=>\"d\", \"a\"=>\"b\")\}\)/)
      end

      it "matches against a Matcher" do
        expect do
          @double.should_receive(:msg).with(equal(3))
          @double.msg(37)
        end.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"double\" received :msg with unexpected arguments\n  expected: (equal 3)\n       got: (37)")
      end

      it "fails no_args with one arg" do
        expect do
          @double.should_receive(:msg).with(no_args)
          @double.msg(37)
        end.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"double\" received :msg with unexpected arguments\n  expected: (no args)\n       got: (37)")
      end

      it "fails hash_including with missing key" do
         expect do
           @double.should_receive(:msg).with(hash_including(:a => 1))
           @double.msg({})
         end.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"double\" received :msg with unexpected arguments\n  expected: (hash_including(:a=>1))\n       got: ({})")
      end

      it "fails with block matchers" do
        expect do
          @double.should_receive(:msg).with {|arg| expect(arg).to eq :received }
          @double.msg :no_msg_for_you
        end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected: :received.*\s*.*got: :no_msg_for_you/)
      end

      it "fails with sensible message when args respond to #description" do
        arg = Class.new do
          def description
          end

          def inspect
            "my_thing"
          end
        end.new

        expect do
          @double.should_receive(:msg).with(3)
          @double.msg arg
        end.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"double\" received :msg with unexpected arguments\n  expected: (3)\n       got: (my_thing)")
      end
    end
  end
end