Skip to content
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

Test2::Mock should optionally track HiRes timestamps for calls #945

Open
afresh1 opened this issue Nov 30, 2021 · 1 comment
Open

Test2::Mock should optionally track HiRes timestamps for calls #945

afresh1 opened this issue Nov 30, 2021 · 1 comment

Comments

@afresh1
Copy link

afresh1 commented Nov 30, 2021

I recently hit a place where I had to mock out two different interfaces, but wanted to confirm that they got called in the correct order.

use Test2::Mock;

my $first = Test2::Mock->new(
    track => 'WithTimestamps', # or some other interface
    class => 'AClass',
    ...,
);

my $second = Test2::Mock->new(
    track => 'WithTimestamps', # or some other interface
    class => 'AnotherClass',
    ...,
);

...; # do stuff that calls mocked methods on both classes.

my @ordered_calls = sort { $a->{ts} <=> $b->{ts} } (
    @{ $first->call_tracking ),
    @{ $second->call_tracking },
);

like \@ordered_calls, [
    { sub_name => ..., args => [ 'AClass' ] },
    { sub_name => ..., args => [ 'AnotherClass' ] },
    { sub_name => ..., args => [ 'AClass' ] },
    ...,
];
@afresh1
Copy link
Author

afresh1 commented Dec 1, 2021

Thinking more on this, I don't actually need the timestamp, perhaps a more flexible solution would be a callback:

my @call_tracking;
my $mock1 = Test2::Mock->new( track => sub { push @call_tracking, $_[0] }, ... );
my $mock2 = Test2::Mock->new( track => sub { push @call_tracking, $_[0] }, ... );
...;

If $_[0] was the ref that was going to be added to call_tracking, someone actually wanting a timestamp could add it themselves.

my $mock = Test2::Mock->new( trace => sub { $_[0]->{ts} = gettimefday }, ... );

@exodist exodist transferred this issue from Test-More/Test2-Suite Aug 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant