vbasky's avatar

Favorite a reply results in failed test

Following Jeffrey's Build a Forum tutorials results in failed test

PHPUnit 8.0.5 by Sebastian Bergmann and contributors.

.F                                                                  2 / 2 (100%)

Time: 347 ms, Memory: 28.00 MB

There was 1 failure:

1) Tests\Feature\FavoritesTest::an_authenticated_user_can_favorite_any_reply
Failed asserting that actual size 0 matches expected size 1.

The test is as follows

    /** @test */

    public function an_authenticated_user_can_favorite_any_reply()
    {
        $this->signIn();

        $reply = create('App\Reply');

        $this->post('replies/' . $reply->id . '/favorites');

        $this->assertCount(1, $reply->favorites);
    }

web.php is as follows

Route::post('replies/{reply}/favorites', 'FavoritesController@store');

And in the controller is the store method as below

FavoritesController.php


   public function store(Reply $reply)
   {

        Favorite::create([
            'user_id' => auth()->user()->id,
            'favorited_id' => $reply->id,
            'favorited' => get_class($reply)
          ]);       

   }

When I dd the Favorite::all() in the test after the post I get a blank array. What could I possibly be doing wrong ?

0 likes
3 replies
vbasky's avatar

Figured out what's causing that issue its the mass assignment issue in my model

Favorite.php

   protected $guarded = [];

This removed the error.

siangboon's avatar

i'm not sure which episode you referring to and I'm bit lazy to dig out too... but maybe can try to get the fresh copy of reply.

    $this->assertCount(1, $reply->fresh()->favorites)

and you may post your issue below the comments section of the episode..

vbasky's avatar

@SIANGBOON - It was episode 18. I wasn't too sure why it didn't work. But I now have it working Favorite::create because of mass exception error, which it didn't throw instead gave me count of 0 when I fetched the count through Favorite::all() it's now sorted

Please or to participate in this conversation.