this is my test:
public function a_user_can_unfollow_others()
{
$ali=$this->SignIn();
$behrooz=factory(User::class)->create();
$this->post($behrooz->profile('follow'));
$this->assertCount(1,$ali->fresh()->follows);
$this->assertDatabaseHas('follows',['following_user_id'=>$behrooz->id]);
$this->post($behrooz->profile('follow'));
$this->assertCount(0,$ali->fresh()->follows);
$this->assertDatabaseMissing('follows',['following_user_id'=>$behrooz->id]);
}
and this is the route:
Route::post('/profile/{user:username}/follow','followController@store');
and this is the controller:
class followController extends Controller
{
public function store(User $user)
{
auth()->user()->toggleFollow($user);
return back();
}
}
finally these are the toggleFollow , follow and unfollow methods:
public function toggleFollow(User $user)
{
if ($this->following($user))
{
$this->unfollow($user);
}
else{
$this->follow($user);
}
}
public function follow(User $user)
{
return $this->follows()->save($user);
}
public function unfollow(User $user)
{
return $this->follows()->detach($user);
}
why this test has the following Error:
- Tests\Unit\UserTest::a_user_can_unfollow_others
Failed asserting that actual size 1 matches expected size 0.