Frederick Cheung
2011-Feb-10 18:40 UTC
[rspec-users] hash form of stub_chain returning nil for chains with shared first message
I''m new using rspec, so please forgive me if I''m barking up
the wrong tree, but...
I''d expect this to pass (using rspec 2.5.1)
describe ''stub_chain'' do
it "returns expected value from two chains with hash" do
subject = Object.new
subject.stub_chain(:msg1, :msg2 => :first)
subject.stub_chain(:msg1, :msg3 => :second)
subject.msg1.msg2.should equal(:first)
subject.msg1.msg3.should equal(:second)
end
end
But it doesn''t - subject.msg1.msg3 returns nil
I think this is because in stub_chain (in
rspec-mocks/lib/rspec/mocks/methods.rb, line 43) there is
if matching_stub = __mock_proxy.__send__(:find_matching_method_stub,
chain[0].to_sym)
chain.shift
matching_stub.invoke.stub_chain(*chain)
else
ie blk isn''t used in any way. Changing this to
matching_stub.invoke.stub_chain(*chain, &blk) makes my example pass
Is this true or am I just misusing rspec?
Fred
David Chelimsky
2011-Feb-10 19:30 UTC
[rspec-users] hash form of stub_chain returning nil for chains with shared first message
On Feb 10, 2011, at 12:40 PM, Frederick Cheung wrote:> I''m new using rspec, so please forgive me if I''m barking up the wrong tree, but... > > I''d expect this to pass (using rspec 2.5.1) > > describe ''stub_chain'' do > it "returns expected value from two chains with hash" do > subject = Object.new > subject.stub_chain(:msg1, :msg2 => :first) > subject.stub_chain(:msg1, :msg3 => :second)This ^^ is not documented to work. See http://relishapp.com/rspec/rspec-mocks/v/2-5/dir/method-stubs/stub-a-chain-of-methods. Try: subject.stub_chain(:msg1, :msg2).and_return(:first) If that works, feel free to submit a feature request to support the format you were using. Cheers, David> subject.msg1.msg2.should equal(:first) > subject.msg1.msg3.should equal(:second) > end > end> > But it doesn''t - subject.msg1.msg3 returns nil > > I think this is because in stub_chain (in rspec-mocks/lib/rspec/mocks/methods.rb, line 43) there is > > if matching_stub = __mock_proxy.__send__(:find_matching_method_stub, chain[0].to_sym) > chain.shift > matching_stub.invoke.stub_chain(*chain) > else > > ie blk isn''t used in any way. Changing this to matching_stub.invoke.stub_chain(*chain, &blk) makes my example pass > Is this true or am I just misusing rspec? > > Fred > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Frederick Cheung
2011-Feb-11 09:51 UTC
[rspec-users] hash form of stub_chain returning nil for chains with shared first message
On Feb 10, 7:30?pm, David Chelimsky <dchelim... at gmail.com> wrote:> On Feb 10, 2011, at 12:40 PM, Frederick Cheung wrote: > > > I''m new using rspec, so please forgive me if I''m barking up the wrong tree, but... > > > I''d expect this to pass (using rspec 2.5.1) > > > describe ''stub_chain'' do > > ?it "returns expected value from two chains with hash" do > > ? ?subject = Object.new > > ? ?subject.stub_chain(:msg1, :msg2 => :first) > > ? ?subject.stub_chain(:msg1, :msg3 => :second) > > This ^^ is not documented to work. Seehttp://relishapp.com/rspec/rspec-mocks/v/2-5/dir/method-stubs/stub-a-.... > > Try: > > ? subject.stub_chain(:msg1, :msg2).and_return(:first) > > If that works, feel free to submit a feature request to support the format you were using. >Fair enough. I''d been reading stub_chain_spec.rb (which does do stuff like @subject.stub_chain(:msg1, :msg2, :msg3, :msg4 => :return_value)) rather than stub_chain.feature and had taken that (as well as https://github.com/rspec/rspec-mocks/commit/2c23b86cc5aaa99557df456054765e95aa1a0d5a ) to mean that subject.stub_chain(:msg1, :msg2 => :first) was official Fred> Cheers, > David > > > > > > > ? ?subject.msg1.msg2.should equal(:first) > > ? ?subject.msg1.msg3.should equal(:second) > > ?end > > end ? > > > But it doesn''t - subject.msg1.msg3 returns nil > > > I think this is because in stub_chain (in rspec-mocks/lib/rspec/mocks/methods.rb, line 43) there is > > > if matching_stub = __mock_proxy.__send__(:find_matching_method_stub, chain[0].to_sym) > > ? ?chain.shift > > ? ?matching_stub.invoke.stub_chain(*chain) > > else > > > ie blk isn''t used in any way. Changing this to matching_stub.invoke.stub_chain(*chain, &blk) makes my example pass > > Is this true or am I just misusing rspec? > > > Fred > > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
David Chelimsky
2011-Feb-12 19:55 UTC
[rspec-users] hash form of stub_chain returning nil for chains with shared first message
On Feb 11, 2011, at 7:51 AM, Frederick Cheung wrote:> On Feb 10, 7:30 pm, David Chelimsky <dchelim... at gmail.com> wrote: >> On Feb 10, 2011, at 12:40 PM, Frederick Cheung wrote: >> >>> I''m new using rspec, so please forgive me if I''m barking up the wrong tree, but... >> >>> I''d expect this to pass (using rspec 2.5.1) >> >>> describe ''stub_chain'' do >>> it "returns expected value from two chains with hash" do >>> subject = Object.new >>> subject.stub_chain(:msg1, :msg2 => :first) >>> subject.stub_chain(:msg1, :msg3 => :second) >> >> This ^^ is not documented to work. Seehttp://relishapp.com/rspec/rspec-mocks/v/2-5/dir/method-stubs/stub-a-.... >> >> Try: >> >> subject.stub_chain(:msg1, :msg2).and_return(:first) >> >> If that works, feel free to submit a feature request to support the format you were using. >> > > Fair enough. I''d been reading stub_chain_spec.rb (which does do stuff > like @subject.stub_chain(:msg1, :msg2, :msg3, :msg4 => :return_value)) > rather than stub_chain.feature and had taken that (as well as > https://github.com/rspec/rspec-mocks/commit/2c23b86cc5aaa99557df456054765e95aa1a0d5a > ) to mean that subject.stub_chain(:msg1, :msg2 => :first) was officialI was wrong. I''d consider this documented, and that what you are experiencing is a bug. Please submit an issue (https://github.com/rspec/rspec-mocks/issues). Thx, David> > Fred > >> Cheers, >> David >> >> >> >> >> >>> subject.msg1.msg2.should equal(:first) >>> subject.msg1.msg3.should equal(:second) >>> end >>> end >> >>> But it doesn''t - subject.msg1.msg3 returns nil >> >>> I think this is because in stub_chain (in rspec-mocks/lib/rspec/mocks/methods.rb, line 43) there is >> >>> if matching_stub = __mock_proxy.__send__(:find_matching_method_stub, chain[0].to_sym) >>> chain.shift >>> matching_stub.invoke.stub_chain(*chain) >>> else >> >>> ie blk isn''t used in any way. Changing this to matching_stub.invoke.stub_chain(*chain, &blk) makes my example pass >>> Is this true or am I just misusing rspec? >> >>> Fred >> >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-us... at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >> >> _______________________________________________ >> rspec-users mailing list >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersCheers, David
David Chelimsky
2011-Feb-12 20:43 UTC
[rspec-users] hash form of stub_chain returning nil for chains with shared first message
On Feb 12, 2011, at 5:55 PM, David Chelimsky wrote:> > On Feb 11, 2011, at 7:51 AM, Frederick Cheung wrote: > >> On Feb 10, 7:30 pm, David Chelimsky <dchelim... at gmail.com> wrote: >>> On Feb 10, 2011, at 12:40 PM, Frederick Cheung wrote: >>> >>>> I''m new using rspec, so please forgive me if I''m barking up the wrong tree, but... >>> >>>> I''d expect this to pass (using rspec 2.5.1) >>> >>>> describe ''stub_chain'' do >>>> it "returns expected value from two chains with hash" do >>>> subject = Object.new >>>> subject.stub_chain(:msg1, :msg2 => :first) >>>> subject.stub_chain(:msg1, :msg3 => :second) >>> >>> This ^^ is not documented to work. Seehttp://relishapp.com/rspec/rspec-mocks/v/2-5/dir/method-stubs/stub-a-.... >>> >>> Try: >>> >>> subject.stub_chain(:msg1, :msg2).and_return(:first) >>> >>> If that works, feel free to submit a feature request to support the format you were using. >>> >> >> Fair enough. I''d been reading stub_chain_spec.rb (which does do stuff >> like @subject.stub_chain(:msg1, :msg2, :msg3, :msg4 => :return_value)) >> rather than stub_chain.feature and had taken that (as well as >> https://github.com/rspec/rspec-mocks/commit/2c23b86cc5aaa99557df456054765e95aa1a0d5a >> ) to mean that subject.stub_chain(:msg1, :msg2 => :first) was official > > I was wrong. I''d consider this documented, and that what you are experiencing is a bug. > > Please submit an issue (https://github.com/rspec/rspec-mocks/issues).FYI - I''ve already fixed this: https://github.com/rspec/rspec-mocks/commit/ca2d83712fe329affbe4334c8c292c40ae74e5ce Feel free to submit an issue anyhow if you like, and I''ll note this commit in the issue. Cheers, David> > Thx, > David > > >> >> Fred >> >>> Cheers, >>> David >>> >>> >>> >>> >>> >>>> subject.msg1.msg2.should equal(:first) >>>> subject.msg1.msg3.should equal(:second) >>>> end >>>> end >>> >>>> But it doesn''t - subject.msg1.msg3 returns nil >>> >>>> I think this is because in stub_chain (in rspec-mocks/lib/rspec/mocks/methods.rb, line 43) there is >>> >>>> if matching_stub = __mock_proxy.__send__(:find_matching_method_stub, chain[0].to_sym) >>>> chain.shift >>>> matching_stub.invoke.stub_chain(*chain) >>>> else >>> >>>> ie blk isn''t used in any way. Changing this to matching_stub.invoke.stub_chain(*chain, &blk) makes my example pass >>>> Is this true or am I just misusing rspec? >>> >>>> Fred
Frederick Cheung
2011-Feb-13 07:41 UTC
[rspec-users] hash form of stub_chain returning nil for chains with shared first message
On Feb 12, 8:43?pm, David Chelimsky <dchelim... at gmail.com> wrote:> > > I was wrong. I''d consider this documented, and that what you are experiencing is a bug. > > > Please submit an issue (https://github.com/rspec/rspec-mocks/issues). > > FYI - I''ve already fixed this:https://github.com/rspec/rspec-mocks/commit/ca2d83712fe329affbe4334c8... > > Feel free to submit an issue anyhow if you like, and I''ll note this commit in the issue. >Sweet! I created https://github.com/rspec/rspec-mocks/issues/issue/38 (and nice to see that the fix was what I thought it was!) Fred