Skip to content

Can't mock implementations of abstract methods required by a Moose role #55

@DrHyde

Description

@DrHyde

If you have a Moose class which consumes a role, and that role in turn requires that the class implement a method foo then you can't use Test::MockModule to over-ride the class's implementation. Attempting to do so can result in errors like:

"Invalid implementation class MyApp::SomeClass: 'MyApp::SomeRole' requires the method 'foo' to be implemented by 'Moose::Meta::Class::ANON::SERIAL::8'. If you imported functions intending to use them as methods, you need to explicitly mark them as such, via Moose::Meta::Class::ANON::SERIAL::8->meta->add_method(foo => ..."

As a work-around, instead of doing this in my tests:

my $mockery = Test::MockModule->new('MyApp::Class');
$mockery->mock( foo => sub { 0 });

I am doing this:

MyApp::Class->meta->add_method(totes_not_a_real_sub => sub { 0 });
my $mockery = Test::MockModule->new('MyApp::Class');
$mockery->mock( foo => \&MyApp::Class::totes_not_a_real_sub);

but obviously I'd prefer to have Test::MockModule do the nasty Moose dance for me automagically. Would you accept a patch for that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions