public
Description: A Rails app that uses voting to identify pain points.
Homepage:
Clone URL: git://github.com/btakita/pain-point.git
commit  ca9a65e7110ccaa37175c741e1cea1aaa9776180
tree    bae7de1b619c3d0296c3953e1890da9262c1c6bd
parent  7ac763592449ef25d8ac2e1dfe6dafa45bf8f86d
pain-point / vendor / plugins / rspec / spec / spec / mocks / any_instance_spec.rb
100644 248 lines (195 sloc) 7.703 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
require File.dirname(__FILE__) + '/../../spec_helper'
 
module Spec
  module Mocks
    module AnyInstance
      describe "any_instance.stub!" do
        before(:each) do
          @klass = Class.new do
            def original_method
              :original_method
            end
          end
        end
 
        it "should return nil with no arguments for the first instance" do
          @klass.any_instance.stub!(:foo)
          object = @klass.new
          object.foo.should be_nil
        end
 
        it "should return nil with no arguments for the second instance" do
          @klass.any_instance.stub!(:foo)
          object = @klass.new
          
          object.foo.should be_nil
          object.foo.should be_nil
        end
 
        it "should use a return value" do
          @klass.any_instance.stub!(:foo).and_return(:bar)
          object = @klass.new
          
          object.foo.should equal(:bar)
        end
 
        it "should use a StandardError when and_raise is given with no arguments" do
          @klass.any_instance.stub!(:foo).and_raise
          object = @klass.new
          
          lambda {
            object.foo
          }.should raise_error(StandardError)
        end
 
        it "should use a custom error when and_raise is given with a class argument" do
          error_class = Class.new(StandardError)
          @klass.any_instance.stub!(:foo).and_raise(error_class)
          object = @klass.new
          
          lambda {
            object.foo
          }.should raise_error(error_class)
        end
 
        it "should use a custom error with a custom message when and_raise is given with a class and message" do
          error_class = Class.new(StandardError)
          messsage = "foobar"
          
          @klass.any_instance.stub!(:foo).and_raise(error_class, messsage)
          object = @klass.new
          
          lambda {
            object.foo
          }.should raise_error(error_class, messsage)
        end
 
        it "should raise with a string when given one" do
          error_message = "Here is my error"
          
          @klass.any_instance.stub!(:foo).and_raise(error_message)
          object = @klass.new
          
          lambda {
            object.foo
          }.should raise_error(error_message)
 
        end
 
        it "should overwrite the value of the original method" do
          @klass.any_instance.stub!(:original_method).and_return :bar
          object = @klass.new
          
          object.original_method.should equal(:bar)
        end
 
        it "should raise an error if the object given is not a class" do
          instance = Object.new
          lambda {
            instance.any_instance.stub!(:baz)
          }.should raise_error(NoMethodError, "undefined method `any_instance' for #{instance}")
        end
 
        describe "resetting" do
          before :each do
            @klass = Class.new do
              def method_with_one_argument(arg1)
                arg1
              end
              
              def method_with_two_arguments(arg1, arg2)
                [arg1, arg2]
              end
              
              def method_which_yields(&blk)
                blk.call
              end
              
              def method_which_yields_one_arg(&blk)
                blk.call(true)
              end
              
              def bar
                :bar_return_value
              end
            end
          end
 
          it "should be able to reset itself" do
            @klass.any_instance.stub!(:bar)
            @klass.__rspec_clear_instances__
            instance = @klass.new
            
            instance.bar.should equal(:bar_return_value)
          end
 
          it "should use one argument" do
            @klass.any_instance.stub!(:method_with_one_argument)
            @klass.__rspec_clear_instances__
            instance = @klass.new
            
            instance.method_with_one_argument(:arg1).should equal(:arg1)
          end
 
          it "should use two arguments" do
            @klass.any_instance.stub!(:method_with_two_arguments)
            @klass.__rspec_clear_instances__
            instance = @klass.new
            
            instance.method_with_two_arguments(:arg1, :arg2).should eql([:arg1, :arg2])
          end
 
          it "should yield to the block" do
            @klass.any_instance.stub!(:method_which_yields)
            @klass.__rspec_clear_instances__
            instance = @klass.new
            
            val = nil
            instance.method_which_yields do
              val = true
            end
            
            val.should be_true
          end
 
          it "should yield to block with one argument" do
            @klass.any_instance.stub!(:method_which_yields_one_arg)
            @klass.__rspec_clear_instances__
            instance = @klass.new
            
            val = nil
            instance.method_which_yields_one_arg do |yield_value|
              val = yield_value
            end
            
            val.should be_true
          end
 
          it "should not leave methods laying around" do
            lambda {
              @klass.any_instance.stub!(:method_which_yields_one_arg)
              @klass.__rspec_clear_instances__
            }.should_not change(@klass, :instance_methods)
          end
        end
 
        describe "in the mock space" do
          before :each do
            klass = MethodStubber
            @stubber = mock(klass, :null_object => true)
            klass.stub!(:new).and_return @stubber
          end
          
          it "should register itself in the global mock space" do
            $rspec_mocks.should_receive(:add).with(@stubber)
            @klass.any_instance.stub!(:foo)
          end
        end
      end
 
      describe "conforming with the other mock types in the mock space" do
        before :each do
          @stubber_class = MethodStubber
          @stubber = @stubber_class.new(Class.new, :foo)
        end
        
        it "should not do anything when calling rspec_verify (it should not be the method inherited from Object)" do
          @stubber.rspec_verify
          @stubber.should be_verified
        end
        
        it "should not be verified if it was not verified" do
          @stubber.should_not be_verified
        end
        
        it "should not use the rspec_reset inherited from Object (reset! should be it's alias)" do
          reset_method = @stubber_class.instance_method(:reset!)
          rspec_reset = @stubber_class.instance_method(:rspec_reset)
          
          reset_method.should == rspec_reset
          rspec_reset.should == reset_method
        end
      end
 
      describe "any_instance, with a block given" do
        before :each do
          @klass = Class.new
        end
        
        it "should stub a method in the block (with no return value)" do
          @klass.any_instance do |instance|
            instance.stub!(:foo)
          end
          
          @klass.new.foo.should be_nil
        end
        
        it "should stub a method in the block (with a return value)" do
          @klass.any_instance do |instance|
            instance.stub!(:foo).and_return(:bar)
          end
          
          @klass.new.foo.should equal(:bar)
        end
        
        it 'should stub multiple methods in the block' do
          @klass.any_instance do |instance|
            instance.stub!(:foo).and_return(:bar)
            instance.stub!(:baz).and_return(:quxx)
          end
          
          @klass.new.foo.should equal(:bar)
          @klass.new.baz.should equal(:quxx)
        end
      end
    end
  end
end