dchelimsky / rspec

Behaviour Driven Development framework for Ruby

This URL has Read+Write access

rspec / spec / spec / mocks / null_object_mock_spec.rb
100644 55 lines (46 sloc) 1.488 kb
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
require File.dirname(__FILE__) + '/../../spec_helper.rb'
 
module Spec
  module Mocks
    describe "a mock acting as a NullObject" do
      before(:each) do
        @mock = Mock.new("null_object", :null_object => true)
      end
 
      it "should allow explicit expectation" do
        @mock.should_receive(:something)
        @mock.something
      end
 
      it "should fail verification when explicit exception not met" do
        lambda do
          @mock.should_receive(:something)
          @mock.rspec_verify
        end.should raise_error(MockExpectationError)
      end
 
      it "should ignore unexpected methods" do
        @mock.random_call("a", "d", "c")
        @mock.rspec_verify
      end
 
      it "should expected message with different args first" do
        @mock.should_receive(:message).with(:expected_arg)
        @mock.message(:unexpected_arg)
        @mock.message(:expected_arg)
      end
 
      it "should expected message with different args second" do
        @mock.should_receive(:message).with(:expected_arg)
        @mock.message(:expected_arg)
        @mock.message(:unexpected_arg)
      end
    end
 
    describe "#null_object?" do
      it "should default to false" do
        obj = mock('anything')
        obj.should_not be_null_object
      end
    end
    
    describe "#as_null_object" do
      it "should set the object to null_object" do
        obj = mock('anything').as_null_object
        obj.should be_null_object
      end
    end
  end
end