Oscar Del Ben
2010-Oct-19 07:56 UTC
[rspec-users] Testing initialize methods and chained methods
I''m having some troubles understanding how to test a couple of things. Usually, if I''m having trouble testing something, it means that my design could probably be improved or changed, but in these cases I think I''m doing the right thing. Here''s the first scenario: class Foo def initialize do_something end end Here I have no idea how to test that the do_something method is called. Another issue I have is with the following code: my_method.another_chained_method.something_else Here I''m having trouble understanding how to test that my_method receives another_chained_method and so on. Any help would be very appreciated. Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20101019/4fff954d/attachment.html>
Justin Ko
2010-Oct-19 13:09 UTC
[rspec-users] Testing initialize methods and chained methods
On Oct 19, 3:56?am, Oscar Del Ben <thehcdrea... at gmail.com> wrote:> I''m having some troubles understanding how to test a couple of things. > Usually, if I''m having trouble testing something, it means that my design > could probably be improved or changed, but in these cases I think I''m doing > the right thing. > > Here''s the first scenario: > > class Foo > ? def initialize > ? ? do_something > ? end > end > > Here I have no idea how to test that the do_something method is called. > > Another issue I have is with the following code: > > my_method.another_chained_method.something_else > > Here I''m having trouble understanding how to test that my_method receives > another_chained_method and so on. > > Any help would be very appreciated. Thanks! > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-usersHow does the initialize method usually get called? By call ".new" on the class. Here is some docs on your new friend "stub_chain" http://relishapp.com/rspec/rspec-mocks/v/2-0/dir/stubs/stub-a-chain-of-methods I''m in a hurry, sorry I can''t help more than that.
Andrew Premdas
2010-Oct-19 17:12 UTC
[rspec-users] Testing initialize methods and chained methods
Set an expectation that do_something should be called once. Then create a new Foo. With the method chain set an expectation that your chained method should be called, then call the original method. You should be able to check that the original method is passed as a param. Further information in RSpec book which has some gr8 stuff on expectations HTH Andrew On 19 October 2010 08:56, Oscar Del Ben <thehcdreamer at gmail.com> wrote:> I''m having some troubles understanding how to test a couple of things. > Usually, if I''m having trouble testing something, it means that my design > could probably be improved or changed, but in these cases I think I''m doing > the right thing. > > Here''s the first scenario: > > class Foo > def initialize > do_something > end > end > > Here I have no idea how to test that the do_something method is called. > > Another issue I have is with the following code: > > my_method.another_chained_method.something_else > > Here I''m having trouble understanding how to test that my_method receives > another_chained_method and so on. > > Any help would be very appreciated. Thanks! > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20101019/4cc3fd84/attachment.html>
Pat Maddox
2010-Oct-19 19:04 UTC
[rspec-users] Testing initialize methods and chained methods
On Oct 19, 2010, at 12:56 AM, Oscar Del Ben wrote:> I''m having some troubles understanding how to test a couple of things. Usually, if I''m having trouble testing something, it means that my design could probably be improved or changed, but in these cases I think I''m doing the right thing. > > Here''s the first scenario: > > class Foo > def initialize > do_something > end > end > > Here I have no idea how to test that the do_something method is called.What are the post-conditions of do_something? Check for those. Pat
David Chelimsky
2010-Oct-19 19:08 UTC
[rspec-users] Testing initialize methods and chained methods
On Oct 19, 2010, at 2:04 PM, Pat Maddox wrote:> On Oct 19, 2010, at 12:56 AM, Oscar Del Ben wrote: > >> I''m having some troubles understanding how to test a couple of things. Usually, if I''m having trouble testing something, it means that my design could probably be improved or changed, but in these cases I think I''m doing the right thing. >> >> Here''s the first scenario: >> >> class Foo >> def initialize >> do_something >> end >> end >> >> Here I have no idea how to test that the do_something method is called. > > What are the post-conditions of do_something? Check for those.Think "outcomes" as opposed to "post-conditions", which means something very specific (and subtly different) in the context of Design by Contract.
Oscar Del Ben
2010-Oct-20 06:55 UTC
[rspec-users] Testing initialize methods and chained methods
Thanks for all the replies and the help. On Tue, Oct 19, 2010 at 9:08 PM, David Chelimsky <dchelimsky at gmail.com>wrote:> On Oct 19, 2010, at 2:04 PM, Pat Maddox wrote: > > > On Oct 19, 2010, at 12:56 AM, Oscar Del Ben wrote: > > > >> I''m having some troubles understanding how to test a couple of things. > Usually, if I''m having trouble testing something, it means that my design > could probably be improved or changed, but in these cases I think I''m doing > the right thing. > >> > >> Here''s the first scenario: > >> > >> class Foo > >> def initialize > >> do_something > >> end > >> end > >> > >> Here I have no idea how to test that the do_something method is called. > > > > What are the post-conditions of do_something? Check for those. > > Think "outcomes" as opposed to "post-conditions", which means something > very specific (and subtly different) in the context of Design by Contract. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- Oscar Del Ben http://oscardelben.com http://freestylemind.com/ http://twitter.com/oscardelben http://facebook.com/freestylemind -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20101020/5f1437b5/attachment.html>