Matt Wynne
2010-Feb-20 22:09 UTC
[rspec-users] Suggested shorthand for and_return when stubbing
I keep wanting to do this, and I don''t think it''s possible so I thought I''d suggest it. Similarly to the way I can specify stub values as hash key/value pairs when constructing a test double, I''d like to be able to the same when subsequently calling stub on that double: me.stub(:name => ''Matt'') That''s as opposed to me.stub(:name).and_return(''Matt'') Thoughts? cheers, Matt http://mattwynne.net +447974 430184
David Chelimsky
2010-Feb-20 23:22 UTC
[rspec-users] Suggested shorthand for and_return when stubbing
On Sat, Feb 20, 2010 at 4:09 PM, Matt Wynne <matt at mattwynne.net> wrote:> I keep wanting to do this, and I don''t think it''s possible so I thought I''d > suggest it. Similarly to the way I can specify stub values as hash key/value > pairs when constructing a test double, I''d like to be able to the same when > subsequently calling stub on that double: > > ? ?me.stub(:name => ''Matt'') > > That''s as opposed to > > ? ?me.stub(:name).and_return(''Matt'') > > Thoughts?Have you tried it? It already works. You can do this, in fact: me.stub(:name => ''David'', :predictor_of_matts_future_requests => true) Also, try this one: me.stub(:name) { ''Matt'' } That''s actually my preference, because it gives you access to args: me.stub(:sum) {|a,b| a + b} me.sum(3,4) => 7 HTH, David
Frank Lakatos
2010-Feb-20 23:27 UTC
[rspec-users] Suggested shorthand for and_return when stubbing
me.stub(:sum) {|a,b| a + b} = really slick trick! On Feb 20, 2010, at 6:22 PM, David Chelimsky wrote:> On Sat, Feb 20, 2010 at 4:09 PM, Matt Wynne <matt at mattwynne.net> > wrote: >> I keep wanting to do this, and I don''t think it''s possible so I >> thought I''d >> suggest it. Similarly to the way I can specify stub values as hash >> key/value >> pairs when constructing a test double, I''d like to be able to the >> same when >> subsequently calling stub on that double: >> >> me.stub(:name => ''Matt'') >> >> That''s as opposed to >> >> me.stub(:name).and_return(''Matt'') >> >> Thoughts? > > Have you tried it? It already works. You can do this, in fact: > > me.stub(:name => ''David'', :predictor_of_matts_future_requests => > true) > > Also, try this one: > > me.stub(:name) { ''Matt'' } > > That''s actually my preference, because it gives you access to args: > > me.stub(:sum) {|a,b| a + b} > me.sum(3,4) > => 7 > > HTH, > David > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
David Chelimsky
2010-Feb-20 23:32 UTC
[rspec-users] Suggested shorthand for and_return when stubbing
On Sat, Feb 20, 2010 at 5:27 PM, Frank Lakatos <me at franklakatos.com> wrote:> ?me.stub(:sum) {|a,b| a + b} = really slick trick!It''s an easy way to do a Fake (http://xunitpatterns.com/Fake%20Object.html), at the method level (all of those patterns are really method level patters, not object level patterns). Another use is for expectations: foo.should_receive(:bar) do |a, b| a.should be_x b.should be_y end> > > On Feb 20, 2010, at 6:22 PM, David Chelimsky wrote: > >> On Sat, Feb 20, 2010 at 4:09 PM, Matt Wynne <matt at mattwynne.net> wrote: >>> >>> I keep wanting to do this, and I don''t think it''s possible so I thought >>> I''d >>> suggest it. Similarly to the way I can specify stub values as hash >>> key/value >>> pairs when constructing a test double, I''d like to be able to the same >>> when >>> subsequently calling stub on that double: >>> >>> ? me.stub(:name => ''Matt'') >>> >>> That''s as opposed to >>> >>> ? me.stub(:name).and_return(''Matt'') >>> >>> Thoughts? >> >> Have you tried it? It already works. You can do this, in fact: >> >> ?me.stub(:name => ''David'', :predictor_of_matts_future_requests => true) >> >> Also, try this one: >> >> ?me.stub(:name) { ''Matt'' } >> >> That''s actually my preference, because it gives you access to args: >> >> ?me.stub(:sum) {|a,b| a + b} >> ?me.sum(3,4) >> ?=> 7 >> >> HTH, >> David >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Matt Wynne
2010-Feb-21 00:15 UTC
[rspec-users] Suggested shorthand for and_return when stubbing
On 20 Feb 2010, at 23:22, David Chelimsky wrote:> On Sat, Feb 20, 2010 at 4:09 PM, Matt Wynne <matt at mattwynne.net> > wrote: >> I keep wanting to do this, and I don''t think it''s possible so I >> thought I''d >> suggest it. Similarly to the way I can specify stub values as hash >> key/value >> pairs when constructing a test double, I''d like to be able to the >> same when >> subsequently calling stub on that double: >> >> me.stub(:name => ''Matt'') >> >> That''s as opposed to >> >> me.stub(:name).and_return(''Matt'') >> >> Thoughts? > > Have you tried it? It already works. You can do this, in fact: > > me.stub(:name => ''David'', :predictor_of_matts_future_requests => > true) > > Also, try this one: > > me.stub(:name) { ''Matt'' } > > That''s actually my preference, because it gives you access to args: > > me.stub(:sum) {|a,b| a + b} > me.sum(3,4) > => 7Ha ha. I should have known :)> > HTH, > David > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-userscheers, Matt http://mattwynne.net +447974 430184