Skip to content
Advertisement

Problem in testing with withConsecutive()

I have a class FillUserPaymentStatisticService with methods:

JavaScript

And I try write test:

JavaScript

And i have a fail:

Parameter 0 for invocation #0 AppRepositoriesMembersBillingUserPaymentsRepository::getSummaryStatistic(Array (…)): array does not match expected value.

Expected: Array ( ‘dateFrom’ => 2020-02-05T00:00:00.000000+0000 ‘dateTo’ => DateTime Object (…) ‘onlyPaid’ => false ) Actual: Array ( ‘dateFrom’ => 2020-02-06T00:00:00.000000+0000 ‘dateTo’ => DateTime Object (…) ‘onlyPaid’ => false )

If today 2020-02-07 then first date must be 2020-02-06 and second 2020-02-05, why in test the date is 2020-02-06?

When I change method fillStatisticForAllTime like:

JavaScript

its become work correct

Advertisement

Answer

The main issue here is that your variables are mutable objects (passed by a reference).

Also, mock assertions are being checked after a test and some of your variables have changed.

FIX: Clone any date (“detach” from the original variable) before using it.

JavaScript

Btw, your 2nd approach works because you create a new object (“detact” from the original variable)

To avoid similar bugs in future you may want to use immutable date objects.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement