Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gizmojo's avatar

Pest Higher Order Expectation for carbon date object

Is it possible to use higher order expectation to compare carbon date object?

E.g

$releaseDate = now();
$product->update(['release_date' => $releaseDate]);

expect($product)
   ->release_date->toBe($releaseDate)

$release_date is cast to date but it's throwing error:

Failed asserting that two variables reference the same object.

Can do it manually comparing the date string but wondering if higher order expectation can handle this.

expect($product->release_date->toDateString())
        ->toBe($releaseDate->toDateString());
0 likes
2 replies
s4muel's avatar
s4muel
Best Answer
Level 50

the objects are never going to be the same, thus it wont work with toBe() to compare the carbons.

but i think you can achieve a very similar to what you want by a slightly tweaked syntax:

    expect($product)
        ->brand->toBe('samsung') //just a dummy in this case, of course, just to show the higher order expectation
        ->release_date->toDateString()->toBe($releaseDate->toDateString())
        ->category->toBe('phone'); //another dummy

give it a shot a let us know

gizmojo's avatar

That works great thanks didn't realise you could do that.

1 like

Please or to participate in this conversation.