[rspec-users] mock object in array is getting size called on it

David Chelimsky dchelimsky at gmail.com
Fri Jun 26 08:37:18 EDT 2009

On Fri, Jun 26, 2009 at 7:13 AM, doug livesey<biot023 at gmail.com> wrote:
> Hi, I have this code:
>   def self.bulk_create_from_holly( params )
>     params.map do |param|
>       new( param )
>     end
>   end
> That is testeb by this expectation:
>     it "should generate a new property with each set of params" do
>       [ @params, @properties ].transpose.each do |(param, property)|
>         Property.should_receive( :new ).with( param ).and_return( property )
>       end
>       do_call
>     end
> In it, @params and @properties are of the same size (they are generated
> together). The objects in the @properties array are not mocked active record
> objects, but just plain old mocks (ie. mock( property1 ) ). This has to be
> so, as I am using a third party ar adapter that does not support the rails
> spec stubs & mocks.
> In the spec, I am simply trying to assert that a new property should be
> generated for each item in @params.
> However, I am getting this message when I run the expectation:
> NoMethodError in 'Property.bulk_create_from_holly( params ) should generate
> a new property with each set of params'
> undefined method `size' for
> #<Proc:0xb72721b4 at ./spec/models/property_spec.rb:49>
> /home/doug/work/rails/neville/app/models/property.rb:20:in
> `bulk_create_from_holly'
> /home/doug/work/rails/neville/app/models/property.rb:19:in `map'
> /home/doug/work/rails/neville/app/models/property.rb:19:in
> `bulk_create_from_holly'
> ./spec/models/property_spec.rb:38:in `do_call'
> ./spec/models/property_spec.rb:52:
> ./script/spec:10:
> As you can see, nowhere do I call size(), and so can only think that this is
> some bizarre internal thing.

Why don't you debug it and find out where the call is coming from? If
you've updated to rspec-1.2.7  you can do this:

prop1 = mock(property1)
prop1.stub(:size) { puts caller(0)[1]; 37 }

Then at least we'll know what we're dealing with :)

> Or I'm being really dumb & missing something obvious.
> Can anyone spot where I'm going wrong, or maybe suggest a way around this?
> Cheers,
>    Doug.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list