guests_may_not_create_threads

Published 3 weeks ago by hjortur17

Hi, I'm having trouble getting me tests to run. It always respond with this:

Failed asserting that exception of type "Illuminate\Database\QueryException" matches expected exception "Illuminate\Auth\AuthenticationException". Message was: "SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: threads.user_id

And this is my code:

/** @test */
    function guests_may_not_create_threads()
    {
        $this->expectException('Illuminate\Auth\AuthenticationException');

        $thread = factory('App\Thread')->make();

        $this->post('/frettir', $thread->toArray());
    }

And the Controller has

public function __contstuct()
       {
              $this->middleware('auth')->only('store');
       }
Best Answer (As Selected By hjortur17)
D9705996

Was confused why middleware wasn't kicking in... there's a typo in this

public function __contstuct()

Should be

public function __construct()
D9705996
D9705996
3 weeks ago (121,180 XP)

Why are you testing that any exception is being thrown. I would test the http response code instead is a 403.

 $thread = factory('App\Thread')->make();
 $this->post('/frettir', $thread->toArray())
   ->assertForbidden(); // add this test
hjortur17

Not sure, I just followed one of the tutorials. I'm just learning how to use the Test.

But now I'm getting another error.

Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: threads.user_id
D9705996
D9705996
3 weeks ago (121,180 XP)

Can you share your controller the /frettirroute hits and you thread and User models

The problem is that you are trying to save a thread but your not assigning a user to the thread

hjortur17

Yeah

Routes

Route::get('/frettir', '[email protected]'); 
Route::post('/frettir', '[email protected]');
Route::get('/frettir/{thread}', '[email protected]'); 
Route::post('/frettir/{thread}/athugasemdir', '[email protected]'); 

Controller

public function index()
       {
              $threads = Thread::latest()->get();
              
              return view('threads.index', compact('threads'));
       }

public function store(Request $request)
       {
              $thread = Thread::create([
                     'title' => request('title'),
                     'body' => request('body'),
                     'user_id' => auth()->id()
              ]);

              return redirect($thread->path());
       }

public function show(Thread $thread)
       {
              return view('threads.show', compact('thread'));
       }

User Model

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

Thread Model

    protected $guarded = [];
    
    public function path()
    {
        return '/frettir/' . $this->id;
    }

    public function replies()
    {
        return $this->hasMany(Reply::class);
    }

    public function addReply($reply)
    {
        $this->replies()->create($reply);
    }

    public function creator()
    {
        return $this->belongsTo(User::class, 'user_id');
    }
D9705996
D9705996
3 weeks ago (121,180 XP)

Was confused why middleware wasn't kicking in... there's a typo in this

public function __contstuct()

Should be

public function __construct()
D9705996
D9705996
3 weeks ago (121,180 XP)

Thanks, because middleware wasn't being applied to prevent unauthorised users and you using auth()->id to assign the user_id it was null and rightly your database won't allow that.

hjortur17

Thanks! Thanks for explaining!!!

Please sign in or create an account to participate in this conversation.