File: nil_expectation_warning_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 (68 lines) | stat: -rw-r--r-- 2,149 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
require 'spec_helper'

def remove_last_describe_from_world
  RSpec::world.example_groups.pop
end

def empty_example_group
  RSpec::Core::ExampleGroup.describe(Object, 'Empty Behaviour Group') { }
  remove_last_describe_from_world
end

module RSpec
  module Mocks
    describe "an expectation set on nil" do
      it "issues a warning with file and line number information" do
        expected_warning = %r%An expectation of :foo was set on nil. Called from #{__FILE__}:#{__LINE__+3}(:in .+)?. Use allow_message_expectations_on_nil to disable warnings.%
        Kernel.should_receive(:warn).with(expected_warning)

        nil.should_receive(:foo)
        nil.foo
      end

      it "issues a warning when the expectation is negative" do
        Kernel.should_receive(:warn)

        nil.should_not_receive(:foo)
      end

      it "does not issue a warning when expectations are set to be allowed" do
        allow_message_expectations_on_nil
        Kernel.should_not_receive(:warn)

        nil.should_receive(:foo)
        nil.should_not_receive(:bar)
        nil.foo
      end

      it 'does not call #nil? on a double extra times' do
        dbl = double
        dbl.should_receive(:nil?).once.and_return(false)
        dbl.nil?
      end
    end

    describe "#allow_message_expectations_on_nil" do
      it "does not affect subsequent examples" do
        example_group = ::RSpec::Core::ExampleGroup.describe
        reporter      = ::RSpec.configuration.reporter
        example_group.it("when called in one example that doesn't end up setting an expectation on nil") do
                        allow_message_expectations_on_nil
                      end
        example_group.it("should not effect the next example ran") do
                        Kernel.should_receive(:warn)
                        nil.should_receive(:foo)
                        nil.foo
                      end

        expect(example_group.run reporter).to eq true
      end

      it 'doesnt error when marshalled' do
        allow_message_expectations_on_nil
        expect(Marshal.dump(nil)).to eq Marshal.dump_without_mocks(nil)
      end
    end
  end
end