mock a method on a single object

Discussion in 'Perl Misc' started by Belebele, Apr 7, 2010.

  1. Belebele

    Belebele Guest

    How can I mock a method on a _single_ object (as opposed to mocking
    the method on all the objects of a class)? Consider:

    package Foo;
    {
    sub new {
    my $class = shift;
    return bless {}, $class;
    }

    sub bar { print "bar\n" }
    }


    my ($a_foo, $another_foo) = (new Foo(), new Foo());
    sub print_something { print "something\n" }
    *Foo::bar = *print_something; # I would like to change the behavior
    for only $a_foo, but not for $another_foo
    $a_foo->bar(); # print_something
    $another_foo->bar(); # print_something

    Thanks
    Belebele, Apr 7, 2010
    #1
    1. Advertising

  2. Belebele

    Willem Guest

    Belebele wrote:
    ) How can I mock a method on a _single_ object (as opposed to mocking
    ) the method on all the objects of a class)? Consider:

    I'm not sure what you mean by 'mocking', but if it means what I think it
    means, I would think that Perl calls that 'blessing' ?

    ) my ($a_foo, $another_foo) = (new Foo(), new Foo());
    ) sub print_something { print "something\n" }
    ) *Foo::bar = *print_something; # I would like to change the behavior
    ) for only $a_foo, but not for $another_foo
    ) $a_foo->bar(); # print_something
    ) $another_foo->bar(); # print_something

    Perhaps something like:

    bless $a_foo, 'Foo::barbaz';

    package Foo::barbaz
    our @ISA = 'Foo::bar';
    sub print_something { print "something\n" }

    would do the trick ?

    Although I'm still not entirely sure what you intend to do.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Apr 7, 2010
    #2
    1. Advertising

  3. Belebele

    Peter Scott Guest

    On Tue, 06 Apr 2010 19:46:35 -0700, Belebele wrote:
    > How can I mock a method on a _single_ object (as opposed to mocking the
    > method on all the objects of a class)? Consider:
    >
    > package Foo;
    > {
    > sub new {
    > my $class = shift;
    > return bless {}, $class;
    > }
    >
    > sub bar { print "bar\n" }
    > }
    >
    >
    > my ($a_foo, $another_foo) = (new Foo(), new Foo()); sub print_something
    > { print "something\n" } *Foo::bar = *print_something; # I would like to
    > change the behavior for only $a_foo, but not for $another_foo
    > $a_foo->bar(); # print_something
    > $another_foo->bar(); # print_something


    $ cat foo
    #!/usr/local/bin/perl
    use strict;
    use warnings;
    use 5.10.0;

    package Foo;
    sub bar { say "bar" }
    sub new { bless {}, shift }

    package main;
    use Test::MockObject::Extends;

    my $x = Foo->new;
    $x = Test::MockObject::Extends->new( $x );
    $x->mock( bar => sub { say "mocked" } );
    my $y = Foo->new;
    $x->bar;
    $y->bar;

    $ ./foo
    mocked
    bar

    --
    Peter Scott
    http://www.perlmedic.com/ http://www.perldebugged.com/
    http://www.informit.com/store/product.aspx?isbn=0137001274
    http://www.oreillyschool.com/courses/perl1/
    Peter Scott, Apr 7, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken
    Replies:
    0
    Views:
    354
  2. John J. Lee

    Mock object creation by example?

    John J. Lee, Jul 9, 2003, in forum: Python
    Replies:
    0
    Views:
    463
    John J. Lee
    Jul 9, 2003
  3. John J. Lee
    Replies:
    4
    Views:
    361
    John J. Lee
    Nov 8, 2003
  4. Remy Blank
    Replies:
    3
    Views:
    866
    Lothar Scholz
    Mar 6, 2004
  5. Czterysta Czwarty
    Replies:
    0
    Views:
    492
    Czterysta Czwarty
    Jun 14, 2010
Loading...

Share This Page