~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to vendor/gems/rspec/spec/spec/mocks/partial_mock_spec.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mto: (3.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20080726154345-1fmgo76b4l72ulvc
ImportĀ upstreamĀ versionĀ 0.24.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require File.dirname(__FILE__) + '/../../spec_helper.rb'
 
2
 
 
3
module Spec
 
4
  module Mocks
 
5
    describe "using a Partial Mock," do
 
6
      before(:each) do
 
7
        @object = Object.new
 
8
      end
 
9
    
 
10
      it "should name the class in the failure message" do
 
11
        @object.should_receive(:foo)
 
12
        lambda do
 
13
          @object.rspec_verify
 
14
        end.should raise_error(Spec::Mocks::MockExpectationError, /Object/)
 
15
      end
 
16
    
 
17
      it "should not conflict with @options in the object" do
 
18
        @object.instance_eval { @options = Object.new }
 
19
        @object.should_receive(:blah)
 
20
        @object.blah
 
21
      end
 
22
            
 
23
      it "should_not_receive should mock out the method" do
 
24
        @object.should_not_receive(:fuhbar)
 
25
        @object.fuhbar
 
26
        lambda do
 
27
          @object.rspec_verify
 
28
        end.should raise_error(Spec::Mocks::MockExpectationError)
 
29
      end
 
30
    
 
31
      it "should_not_receive should return a negative message expectation" do
 
32
        @object.should_not_receive(:foobar).should be_kind_of(NegativeMessageExpectation)
 
33
      end
 
34
    
 
35
      it "should_receive should mock out the method" do
 
36
        @object.should_receive(:foobar).with(:test_param).and_return(1)
 
37
        @object.foobar(:test_param).should equal(1)
 
38
      end
 
39
    
 
40
      it "should_receive should handle a hash" do
 
41
        @object.should_receive(:foobar).with(:key => "value").and_return(1)
 
42
        @object.foobar(:key => "value").should equal(1)
 
43
      end
 
44
    
 
45
      it "should_receive should handle an inner hash" do
 
46
        hash = {:a => {:key => "value"}}
 
47
        @object.should_receive(:foobar).with(:key => "value").and_return(1)
 
48
        @object.foobar(hash[:a]).should equal(1)
 
49
      end
 
50
    
 
51
      it "should_receive should return a message expectation" do
 
52
        @object.should_receive(:foobar).should be_kind_of(MessageExpectation)
 
53
        @object.foobar
 
54
      end
 
55
    
 
56
      it "should_receive should verify method was called" do
 
57
        @object.should_receive(:foobar).with(:test_param).and_return(1)
 
58
        lambda do
 
59
          @object.rspec_verify
 
60
        end.should raise_error(Spec::Mocks::MockExpectationError)
 
61
      end
 
62
    
 
63
      it "should_receive should also take a String argument" do
 
64
        @object.should_receive('foobar')
 
65
        @object.foobar
 
66
      end
 
67
      
 
68
      it "should_not_receive should also take a String argument" do
 
69
        @object.should_not_receive('foobar')
 
70
        @object.foobar
 
71
        lambda do
 
72
          @object.rspec_verify
 
73
        end.should raise_error(Spec::Mocks::MockExpectationError)
 
74
      end
 
75
      
 
76
      it "should use report nil in the error message" do
 
77
        @this_will_resolve_to_nil.should_receive(:foobar)
 
78
        lambda do
 
79
          @this_will_resolve_to_nil.rspec_verify
 
80
        end.should raise_error(Spec::Mocks::MockExpectationError, /NilClass.*expected :foobar with/)
 
81
      end
 
82
    end
 
83
    
 
84
    describe "Partially mocking an object that defines ==, after another mock has been defined" do
 
85
      before(:each) do
 
86
        stub("existing mock", :foo => :foo)
 
87
      end
 
88
      
 
89
      class PartiallyMockedEquals
 
90
        attr_reader :val
 
91
        def initialize(val)
 
92
          @val = val
 
93
        end
 
94
        
 
95
        def ==(other)
 
96
          @val == other.val
 
97
        end
 
98
      end
 
99
      
 
100
      it "should not raise an error when stubbing the object" do
 
101
        o = PartiallyMockedEquals.new :foo
 
102
        lambda { o.stub!(:bar) }.should_not raise_error(NoMethodError)
 
103
      end
 
104
    end
 
105
  end
 
106
end