David Chelimsky
2009-Jun-26 12:37 UTC
[rspec-users] mock object in array is getting size called on it
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 >
David Chelimsky
2009-Jun-26 13:24 UTC
[rspec-users] mock object in array is getting size called on it
On Fri, Jun 26, 2009 at 8:15 AM, doug livesey<biot023 at gmail.com> wrote:>> Why don''t you debug it and find out where the call is coming from? > > I get: > > /home/doug/work/rails/neville/vendor/gems/rspec-1.2.7/lib/spec/mocks/message_expectation.rb:171:in > `call'' > > That block''s a very useful feature I was unaware of! ;)Yeah - it''s in the release notes, but I should really blog about it. Maybe next year :) Anyhow - that''s the code that''s calling the block I asked you to put in there :) Doesn''t help much - try going back a few frames in the stack: prop1 = mock(property1) prop1.stub(:size) { puts caller(0)[1..5]; 37 }> ?? Doug. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
David Chelimsky
2009-Jun-26 13:34 UTC
[rspec-users] mock object in array is getting size called on it
On Fri, Jun 26, 2009 at 8:30 AM, doug livesey<biot023 at gmail.com> wrote:> Copying that (a-la monkey see, monkey do), I get: > > /home/doug/work/rails/neville/vendor/gems/rspec-1.2.7/lib/spec/mocks/message_expectation.rb:171:in > `call'' > /home/doug/work/rails/neville/vendor/gems/rspec-1.2.7/lib/spec/mocks/message_expectation.rb:171:in > `invoke_return_block'' > /home/doug/work/rails/neville/vendor/gems/rspec-1.2.7/lib/spec/mocks/message_expectation.rb:122:in > `invoke'' > /home/doug/work/rails/neville/vendor/gems/rspec-1.2.7/lib/spec/mocks/proxy.rb:99:in > `message_received'' > /home/doug/work/rails/neville/vendor/gems/rspec-1.2.7/lib/spec/mocks/proxy.rb:142:in > `size''Ah - so close!!!!!! Try [1..10] - I''m pretty sure that the next line will reveal our culprit, as that will show the code that is actually calling the size method.
David Chelimsky
2009-Jun-26 14:44 UTC
[rspec-users] mock object in array is getting size called on it
On Fri, Jun 26, 2009 at 9:36 AM, doug livesey<biot023 at gmail.com> wrote:> That''ll be: > > /home/doug/work/rails/neville/vendor/gems/rspec-1.2.7/lib/spec/mocks/message_expectation.rb:171:in > `call'' > /home/doug/work/rails/neville/vendor/gems/rspec-1.2.7/lib/spec/mocks/message_expectation.rb:171:in > `invoke_return_block'' > /home/doug/work/rails/neville/vendor/gems/rspec-1.2.7/lib/spec/mocks/message_expectation.rb:122:in > `invoke'' > /home/doug/work/rails/neville/vendor/gems/rspec-1.2.7/lib/spec/mocks/proxy.rb:99:in > `message_received'' > /home/doug/work/rails/neville/vendor/gems/rspec-1.2.7/lib/spec/mocks/proxy.rb:142:in > `size'' > /home/doug/work/rails/neville/vendor/gems/rspec-1.2.7/lib/spec/mocks/message_expectation.rb:162:in > `invoke_consecutive_return_block'' > /home/doug/work/rails/neville/vendor/gems/rspec-1.2.7/lib/spec/mocks/message_expectation.rb:120:in > `invoke'' > /home/doug/work/rails/neville/vendor/gems/rspec-1.2.7/lib/spec/mocks/proxy.rb:101:in > `message_received'' > /home/doug/work/rails/neville/vendor/gems/rspec-1.2.7/lib/spec/mocks/proxy.rb:142:in > `new'' > /home/doug/work/rails/neville/app/models/property.rb:20:in > `bulk_create_from_holly''This last line is where you should look, if you haven''t already.> Sorry for the delays -- keep getting called away into other things! > & thanks, > ?? Doug.