behrooznik's avatar

why this test is not working?

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:

  1. Tests\Unit\UserTest::a_user_can_unfollow_others Failed asserting that actual size 1 matches expected size 0.
0 likes
11 replies
tykus's avatar

Can you please format you code blocks properly; wrap each snippet inside triple backticks (```)?

Also, it appears you are taking the same action twice: $this->post($behrooz->profile('follow'));` - is this intended to toggle the follow/unfollow behaviour.

SilenceBringer's avatar

@behrooznik I think the problem is here

public function follow(User $user) { return $this->follows()->save($user); }

when you works with pivot table, you need to use attach method

public function follow(User $user) {
    return $this->follows()->attach($user->id);
}
behrooznik's avatar

Sorry for the code blocks. I wont repeat that mistake. promise.

toggleFollow() does both functionality of follow/ unfollow behavior. I assert the first post request gives me follow behavior and the second time after checking the following status, gives me the unfollow behavior.

behrooznik's avatar

I changed my code by using attach method but the test is failing in the same way

behrooznik's avatar

the behavior of the code in the browser is okay and it is working with both of them(save() and attach()) but the test is failing

behrooznik's avatar

follows() and following() methods:

 public function following(User $user)
    {
        return $this->follows->contains($user);
    }

 public function follows()
    {
        return $this->belongsToMany(User::class, 'follows', 'user_id', 'following_user_id');
    }
SilenceBringer's avatar

@behrooznik ok let's dump into data dumping

public function a_user_can_unfollow_others()
    {
        $ali=$this->SignIn();
        $behrooz=factory(User::class)->create();

dump($behrooz->profile('follow'));

dump($ali->following($behrooz));

dump($ali->fresh()->follows);

        $this->post($behrooz->profile('follow'));

dump($ali->following($behrooz));

dump($ali->fresh()->follows);

        $this->post($behrooz->profile('follow'));

dump($ali->following($behrooz));

dd($ali->fresh()->follows);

    }
behrooznik's avatar

@silencebringer .....................................E...................."/profile/stracke.devon/follow" false Illuminate\Database\Eloquent\Collection^ {#10080 #items: [] } false Illuminate\Database\Eloquent\Collection^ {#3319 #items: array:1 [ 0 => App\User^ {#4990 #fillable: array:4 [ 0 => "username" 1 => "name" 2 => "email" 3 => "password" ] #hidden: array:2 [ 0 => "password" 1 => "remember_token" ] #casts: array:1 [ "email_verified_at" => "datetime" ] #connection: "sqlite" #table: "users" #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:10 [ "id" => "2" "username" => "stracke.devon" "name" => "Miss Berniece Rutherford IV" "avatar" => null "email" => "[email protected]" "email_verified_at" => "2020-10-26 08:12:18" "password" => "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi" "remember_token" => "1CkNDFbwr4" "created_at" => "2020-10-26 08:12:18" "updated_at" => "2020-10-26 08:12:18" ] #original: array:12 [ "id" => "2" "username" => "stracke.devon" "name" => "Miss Berniece Rutherford IV" "avatar" => null "email" => "[email protected]" "email_verified_at" => "2020-10-26 08:12:18" "password" => "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi" "remember_token" => "1CkNDFbwr4" "created_at" => "2020-10-26 08:12:18" "updated_at" => "2020-10-26 08:12:18" "pivot_user_id" => "1" "pivot_following_user_id" => "2" ] #changes: [] #classCastCache: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: array:1 [ "pivot" => Illuminate\Database\Eloquent\Relations\Pivot^ {#9631 +incrementing: false #guarded: [] #connection: "sqlite" #table: "follows" #primaryKey: "id" #keyType: "int" #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:2 [ "user_id" => "1" "following_user_id" => "2" ] #original: array:2 [ "user_id" => "1" "following_user_id" => "2" ] #changes: [] #casts: [] #classCastCache: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: [] #touches: [] +timestamps: false #hidden: [] #visible: [] #fillable: [] +pivotParent: App\User^ {#2817 #fillable: array:4 [ 0 => "username" 1 => "name" 2 => "email" 3 => "password" ] #hidden: array:2 [ 0 => "password" 1 => "remember_token" ] #casts: array:1 [ "email_verified_at" => "datetime" ] #connection: "sqlite" #table: "users" #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:10 [ "id" => "1" "username" => "pacocha.carolina" "name" => "Emmy Frami" "avatar" => null "email" => "[email protected]" "email_verified_at" => "2020-10-26 08:12:18" "password" => "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi" "remember_token" => "tW01M4Lrid" "created_at" => "2020-10-26 08:12:18" "updated_at" => "2020-10-26 08:12:18" ] #original: array:10 [ "id" => "1" "username" => "pacocha.carolina" "name" => "Emmy Frami" "avatar" => null "email" => "[email protected]" "email_verified_at" => "2020-10-26 08:12:18" "password" => "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi" "remember_token" => "tW01M4Lrid" "created_at" => "2020-10-26 08:12:18" "updated_at" => "2020-10-26 08:12:18" ] #changes: [] #classCastCache: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: array:1 [ "follows" => Illuminate\Database\Eloquent\Collection^ {#3319} ] #touches: [] +timestamps: true #visible: [] #guarded: array:1 [ 0 => "" ] #rememberTokenName: "remember_token" } #foreignKey: "user_id" #relatedKey: "following_user_id" } ] #touches: [] +timestamps: true #visible: [] #guarded: array:1 [ 0 => "" ] #rememberTokenName: "remember_token" } ] } false Illuminate\Database\Eloquent\Collection^ {#8396 #items: array:1 [ 0 => App\User^ {#2676 #fillable: array:4 [ 0 => "username" 1 => "name" 2 => "email" 3 => "password" ] #hidden: array:2 [ 0 => "password" 1 => "remember_token" ] #casts: array:1 [ "email_verified_at" => "datetime" ] #connection: "sqlite" #table: "users" #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:10 [ "id" => "2" "username" => "stracke.devon" "name" => "Miss Berniece Rutherford IV" "avatar" => null "email" => "[email protected]" "email_verified_at" => "2020-10-26 08:12:18" "password" => "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi" "remember_token" => "1CkNDFbwr4" "created_at" => "2020-10-26 08:12:18" "updated_at" => "2020-10-26 08:12:18" ] #original: array:12 [ "id" => "2" "username" => "stracke.devon" "name" => "Miss Berniece Rutherford IV" "avatar" => null "email" => "[email protected]" "email_verified_at" => "2020-10-26 08:12:18" "password" => "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi" "remember_token" => "1CkNDFbwr4" "created_at" => "2020-10-26 08:12:18" "updated_at" => "2020-10-26 08:12:18" "pivot_user_id" => "1" "pivot_following_user_id" => "2" ] #changes: [] #classCastCache: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: array:1 [ "pivot" => Illuminate\Database\Eloquent\Relations\Pivot^ {#6036 +incrementing: false #guarded: [] #connection: "sqlite" #table: "follows" #primaryKey: "id" #keyType: "int" #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:2 [ "user_id" => "1" "following_user_id" => "2" ] #original: array:2 [ "user_id" => "1" "following_user_id" => "2" ] #changes: [] #casts: [] #classCastCache: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: [] #touches: [] +timestamps: false #hidden: [] #visible: [] #fillable: [] +pivotParent: App\User^ {#2667 #fillable: array:4 [ 0 => "username" 1 => "name" 2 => "email" 3 => "password" ] #hidden: array:2 [ 0 => "password" 1 => "remember_token" ] #casts: array:1 [ "email_verified_at" => "datetime" ] #connection: "sqlite" #table: "users" #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:10 [ "id" => "1" "username" => "pacocha.carolina" "name" => "Emmy Frami" "avatar" => null "email" => "[email protected]" "email_verified_at" => "2020-10-26 08:12:18" "password" => "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi" "remember_token" => "tW01M4Lrid" "created_at" => "2020-10-26 08:12:18" "updated_at" => "2020-10-26 08:12:18" ] #original: array:10 [ "id" => "1" "username" => "pacocha.carolina" "name" => "Emmy Frami" "avatar" => null "email" => "[email protected]" "email_verified_at" => "2020-10-26 08:12:18" "password" => "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi" "remember_token" => "tW01M4Lrid" "created_at" => "2020-10-26 08:12:18" "updated_at" => "2020-10-26 08:12:18" ] #changes: [] #classCastCache: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: array:1 [ "follows" => Illuminate\Database\Eloquent\Collection^ {#8396} ] #touches: [] +timestamps: true #visible: [] #guarded: array:1 [ 0 => "" ] #rememberTokenName: "remember_token" } #foreignKey: "user_id" #relatedKey: "following_user_id" } ] #touches: [] +timestamps: true #visible: [] #guarded: array:1 [ 0 => "" ] #rememberTokenName: "remember_token" } ] }

behrooznik's avatar

@silencebringer it seems that the togglefollow() method is not working properly. because the follow() and unfollow() methods were working when I used them in the test instead of post request

Please or to participate in this conversation.