vendredi 13 juillet 2018

Failing test that record saves to pivot table in laravel

I have a Board model and a Pin model, and they are many to many. I am testing to make sure that when a pin gets created and assigned a board, it saves to the pivot table. I am having trouble figuring this out since I am new to testing.

Here is my Test:

/** @test */
public function a_pin_can_belong_to_a_board()
{
    $board = create('App\Board');
    $pin = make('App\Pin');

    $response = $this->post('/pins/create', $pin->toArray());

    $pin->boards()->sync($board);

    $this->assertDatabaseHas('board_pin', [
        'board_id' => $board->id,
        'pin_id' => $pin->id
    ]);
}

Here is my Error:

1) Tests\Unit\PinTest::a_pin_can_belong_to_a_board
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity 
constraint violation: 19 NOT NULL constraint failed: board_pin.pin_id 
(SQL: insert into "board_pin" ("board_id", "pin_id") values (1, ))

Caused by
PDOException: SQLSTATE[23000]: Integrity constraint violation: 19 NOT 
NULL constraint failed: board_pin.pin_id

Here is my Controller:

public function store(Request $request)
{
    $this->validate($request, [
        'title' => 'required|max:200',
        'link' => 'required|max:255'
    ]);

    $pin = new Pin();

    $pin->title = $request->title;
    $pin->link = $request->link;
    $pin->user_id = auth()->id();

    $pin->save();

    $pin->boards()->sync($request->boards);

    return redirect("/pins/{$pin->id}");
}

Any help would be greatly appreciated! Thank you!

Aucun commentaire:

Enregistrer un commentaire