thebigk's avatar
Level 13

Why is this test failing, when 'found' shows the expected record?

I'm pasting the output of phpStorm exception obtained from phpUnit:

Failed asserting that a row in the table [questions] matches the attributes [
    "title",
    "Rerum quis dolores quas sint repudiandae qui."
].

Found: [
    {
        "id": "1",
        "user_id": "2",
        "title": "Rerum quis dolores quas sint repudiandae qui.",
        "body": "Nam sunt consequuntur non aut exercitationem est earum omnis. Fugiat voluptatibus eveniet laborum sunt quo debitis eveniet nam. Quo dolores itaque similique consequatur natus ipsa inventore.",
        "opt1": "ipsum",
        "opt2": "nulla",
        "opt3": "deserunt",
        "opt4": "quaerat",
        "correct": "4",
        "explanation": "Et ratione ea deserunt et. Qui esse quia pariatur sequi nesciunt quasi. Voluptatem sint veniam soluta id commodi ducimus. Et et nesciunt enim esse.",
        "created_at": "2017-09-27 05:56:54",
        "updated_at": "2017-09-27 05:56:54"
    }
].

AS you can see, the 'title' field in my table actually has the required test. Yet, phpUnit informs that the test failed.

Here's how my test looks like -

/** @test */
    public function an_authenticated_user_can_add_question() {
        //$this->withoutExceptionHandling();
        $this->signIn();
        $question = create('App\Question');
        $this->post('questions/create', $question->toArray() );
        $this->assertDatabaseHas('questions', ['title', $question->title]);
    }

Would really appreciate your help. Thanks!

0 likes
1 reply
thebigk's avatar
Level 13

Update:

Fixed it myself. First, I had to enable withoutExceptionHandling() and then and to run the individual test. I found out that the issue was at two places:

  1. I had not specified $guarded = [];
  2. $this->assertDatabaseHas('questions', ['title', $question->title]); should be $this->assertDatabaseHas('questions', ['title' => $question->title]);

Please or to participate in this conversation.