finchy70's avatar

Test post doesn't hit endpoint

Here is my route.

Route::post('/likes/{reply}', 'LikeController@store')->name('likes.store');

Here is my test

/** @test */
    public function an_authenticated_user_can_like_any_reply()
    {
        $this->signIn();

        $reply = create(Reply::class);

        $this->withoutExceptionHandling()->post('likes/'.$reply->id);

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

Here is my controller method

public function store(Reply $reply)
    {
        dd("hit");
        return DB::table('likes')->insert([
            'user_id' => auth()->user()->id,
            'liked_id' => $reply->id,
            'liked_type' => get_class($reply)
        ]);

    }

When I run my test I get the following error. ReflectionException : Class App\Http\Controllers\Reply does not exist

As I understand ReflectionExceptions are usually caused by errors in the routes file.

I have checked my route and all seams good.

I also tried posting with exception handling on and I get a fail assertion error. Either way the controller method never gets hit as I have a dd("hit"); which is never triggered.

I am on Laravel 7. Does anyone know what I am doing wrong?

0 likes
2 replies
zoltiecodes's avatar
Level 14

This means you forgot to import your Reply class at the top of your controller class. So you miss this line:

use App\Reply;

If your Reply model is in your app directory.

finchy70's avatar

I cant believe I missed that. Thanks so much.

1 like

Please or to participate in this conversation.