FAQ
Hello,

I have a model (MyApp/Models/Bar.pm) which retrieves data from the db using
the generated schema files (in MyApp/Schema/Result/*.pm).

My plan is to mock the classes which are called when retrieving data from
the database.

BEGIN {
my $resultset_mock = Test::MockObject->new();
$resultset_mock->fake_module('DBIx::Class::ResultSet' => ( 'count' =>
sub { 0 } ));

my $pin_attempt_mock = Test::MockObject->new();
$pin_attempt_mock->fake_module('MyApp::Schema::Result::Foo' =>
('search' => sub { bless $resultset_mock, 'DBIx::Class::ResultSet' }));
}


BEGIN { use_ok 'MyApp::Model::Bar' }

is (MyApp::Model::Bar->_get_failed_challenges('test', 'test'),
0,
'get failed challenges'
);

MyApp::Model::Bar->_get_failed_challenges contains calls to search and count
(shown below)

my $result = $c->model('Foo')->search({...});
$result->count();

If I call MyApp::Schema::Result::Foo->search()->count() the mock returns 0
(and other values that I tested). However when I attempt to use test the Bar
model the following error is returned.

"You must pass a package name and it cannot be blessed at
/usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi/Class/MOP/Class.pm
line 41"

Also if I follow the stack trace down into the MyApp files, it points to the
line in MyApp::Model::Bar->_get_failed_challenges which calls search.

Any help would be appreciated!

Thanks,

-- Frank
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20110429/9930c34f/attachment.htm

Search Discussions

  • Trevor Leffler at Apr 29, 2011 at 7:14 pm
    Hi Frank,

    Have you mocked $c->model so that you get your mock, $pin_attempt_mock,
    when you call $c->model('Foo') ?

    You can either create a MockObject for $c, or you can locally override
    $c's model method. I tend to do the former.

    --Trevor


    Franklin Kumro Jr wrote:
    Hello,

    I have a model (MyApp/Models/Bar.pm) which retrieves data from the db
    using the generated schema files (in MyApp/Schema/Result/*.pm).

    My plan is to mock the classes which are called when retrieving data
    from the database.

    BEGIN {
    my $resultset_mock = Test::MockObject->new();
    $resultset_mock->fake_module('DBIx::Class::ResultSet' => ( 'count'
    => sub { 0 } ));

    my $pin_attempt_mock = Test::MockObject->new();
    $pin_attempt_mock->fake_module('MyApp::Schema::Result::Foo' =>
    ('search' => sub { bless $resultset_mock, 'DBIx::Class::ResultSet' }));
    }


    BEGIN { use_ok 'MyApp::Model::Bar' }

    is (MyApp::Model::Bar->_get_failed_challenges('test', 'test'),
    0,
    'get failed challenges'
    );

    MyApp::Model::Bar->_get_failed_challenges contains calls to search and
    count (shown below)

    my $result = $c->model('Foo')->search({...});
    $result->count();

    If I call MyApp::Schema::Result::Foo->search()->count() the mock returns
    0 (and other values that I tested). However when I attempt to use test
    the Bar model the following error is returned.

    "You must pass a package name and it cannot be blessed at
    /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi/Class/MOP/Class.pm
    line 41"

    Also if I follow the stack trace down into the MyApp files, it points to
    the line in MyApp::Model::Bar->_get_failed_challenges which calls search.

    Any help would be appreciated!

    Thanks,

    -- Frank
  • Frank Kumro at May 3, 2011 at 3:53 pm
    Trevor,

    Thanks for the tip, I ended up overriding the model method. Works like a
    charm ;)

    -- Frank

    “Don’t worry if it doesn’t work right. If everything did, you’d be out of a
    job.� (Mosher’s Law of Software Engineering)

    On Fri, Apr 29, 2011 at 3:14 PM, Trevor Leffler wrote:

    Hi Frank,

    Have you mocked $c->model so that you get your mock, $pin_attempt_mock,
    when you call $c->model('Foo') ?

    You can either create a MockObject for $c, or you can locally override $c's
    model method. I tend to do the former.

    --Trevor



    Franklin Kumro Jr wrote:
    Hello,

    I have a model (MyApp/Models/Bar.pm) which retrieves data from the db
    using the generated schema files (in MyApp/Schema/Result/*.pm).

    My plan is to mock the classes which are called when retrieving data from
    the database.

    BEGIN {
    my $resultset_mock = Test::MockObject->new();
    $resultset_mock->fake_module('DBIx::Class::ResultSet' => ( 'count' =>
    sub { 0 } ));

    my $pin_attempt_mock = Test::MockObject->new();
    $pin_attempt_mock->fake_module('MyApp::Schema::Result::Foo' =>
    ('search' => sub { bless $resultset_mock, 'DBIx::Class::ResultSet' }));
    }


    BEGIN { use_ok 'MyApp::Model::Bar' }

    is (MyApp::Model::Bar->_get_failed_challenges('test', 'test'),
    0,
    'get failed challenges'
    );

    MyApp::Model::Bar->_get_failed_challenges contains calls to search and
    count (shown below)

    my $result = $c->model('Foo')->search({...});
    $result->count();

    If I call MyApp::Schema::Result::Foo->search()->count() the mock returns 0
    (and other values that I tested). However when I attempt to use test the Bar
    model the following error is returned.

    "You must pass a package name and it cannot be blessed at
    /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi/Class/MOP/Class.pm
    line 41"

    Also if I follow the stack trace down into the MyApp files, it points to
    the line in MyApp::Model::Bar->_get_failed_challenges which calls search.

    Any help would be appreciated!

    Thanks,

    -- Frank

    _______________________________________________
    List: Catalyst@lists.scsys.co.uk
    Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
    Searchable archive:
    http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
    Dev site: http://dev.catalyst.perl.org/
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20110503/4d7f9cd6/attachment.htm

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedApr 29, '11 at 6:05p
activeMay 3, '11 at 3:53p
posts3
users2
websitecatalystframework.org
irc#catalyst

2 users in discussion

Frank Kumro: 2 posts Trevor Leffler: 1 post

People

Translate

site design / logo © 2021 Grokbase