Level 104
Whenever you first get $john->follows, that result is cached on $john, so you need to get fresh instance after taking the action:
public function a_user_can_toggle_following() {
$john=$this->SignIn();
$this->assertCount(0, $john->follows );
$sally=factory(User::class)->create();
$nick=factory(User::class)->create();
$john->follow($sally);
$this->assertCount(1, $john->fresh()->follows ); // fresh instance
$john->follow($nick);
$this->assertCount(2, $john->fresh()->follows ); // fresh instance
$john->unfollow($nick);
$this->assertCount(1, $john->fresh()->follows ); // fresh instance
}
You could use the query builder either:
public function a_user_can_toggle_following() {
$john=$this->SignIn();
$this->assertEquals(0, $john->follows()->count() );
$sally=factory(User::class)->create();
$nick=factory(User::class)->create();
$john->follow($sally);
$this->assertEquals(1, $john->follows()->count() ); // fresh query
$john->follow($nick);
$this->assertEquals(2, $john->follows()->count() ); // fresh query
$john->unfollow($nick);
$this->assertEquals(1, $john->follows()->count() ); // fresh query
}
1 like