-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't mock implementations of abstract methods required by a Moose role #55
Comments
Sure!
…Sent from my iPhone
On Mar 12, 2021, at 10:11 AM, David Cantrell ***@***.***> wrote:
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?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
FYI I'm having trouble coming up with a small test case without pulling in much of my app's code. There's definitely a problem, but it's unlikely to affect many people. |
Note to self, copied from IRC:
|
the above is me, if you want a test case to reproduce... |
Yes please @fleetfootmike! |
t/mockmodule_around.t
TestA.pm
TestB.pm
|
will mock the "around"ed
t/mockmodule_around.t passes. |
Slight correction: If
|
If you have a Moose class which consumes a role, and that role in turn
requires
that the class implement a methodfoo
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:
I am doing this:
but obviously I'd prefer to have Test::MockModule do the nasty Moose dance for me automagically. Would you accept a patch for that?
The text was updated successfully, but these errors were encountered: