Level 15
Because your $reply instance is "unaware" of the changes you've introduced.
You can get a fresh instance using $reply->fresh(), so you end up with:
$this->assertCount(1, $reply->fresh()->favorites);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm trying to follow the topic Build the forum with TDD approach. You know that PHPUnit is very important to display errors when we running a TestCase.
The problem is when I'm running test by command line is work but when I'm trying to run the test with PHPStorm, it works incorrectly.
This code:
/** @test */
public function an_authenticated_user_only_favorite_reply_once_time()
{
$this->signIn();
$reply = create(Reply::class);
// Like a reply multiple time (store favorites)
$this->post('replies/' . $reply->id . '/favorites');
$this->post('replies/' . $reply->id . '/favorites');
$this->assertCount(1, $reply->favorites);
}
and PHPStorm message show:
C:\laragon\bin\php\php-7.1.12-Win32-VC14-x64\php.exe E:/Coding/PG/ATeam/forum/vendor/phpunit/phpunit/phpunit --no-configuration --filter "/::an_authenticated_user_only_favorite_reply_once_time( .*)?$/" Tests\Feature\FavoritesTest E:\Coding\PG\ATeam\forum\tests\Feature\FavoritesTest.php --teamcity
PHPUnit 7.0.2 by Sebastian Bergmann and contributors.
Failed asserting that actual size 0 matches expected size 1.
E:\Coding\PG\ATeam\forum\tests\Feature\FavoritesTest.php:44
Anyone can tell me why?
Because your $reply instance is "unaware" of the changes you've introduced.
You can get a fresh instance using $reply->fresh(), so you end up with:
$this->assertCount(1, $reply->fresh()->favorites);
Please or to participate in this conversation.