Brad Goldsmith
Brad Goldsmith

Reputation: 293

Unit Testing Laravel need help on how to pass my variables properly

So I'm getting into Unit Testing and I have created a test for creating and adjustment line in my DB. Here is the code:

        $response = $this->json('POST', '/quotes/3/adjustment',
               [
                    'adjustments' => array([
                        'description'   => 'TEST-Description',
                        'amount'        => 1000,
                        'quote_id'      => 3
                    ])
                ]
    );
    $response->assertStatus(201);

When creating it hits my controller then an instance of my Adjustment model is created, and in that model I have this code for the creating of it:

            foreach($request->adjustments as $adjustment) {
            if(array_key_exists('id', $adjustment)) {
                $this->find($adjustment['id'])->update([
                    'description'   => $adjustment['description'],
                    'amount'        => $adjustment['amount'],
                    'quote_id'      => $quote->id
                ]);
            } else {
                $this->create([
                    'description'   => $adjustment['description'],
                    'amount'        => $adjustment['amount'],
                    'quote_id'      => $quote->id,
                ]);
            }
        }
        return $quote;

So it expects adjustments to be an array and I thought I had it coded properly in the test but i get back a 200 response, which is not the 201 as expected. Any ideas on how to properly pass the single array in my test file so that it passes the test?

Here is my controller:

        $adjustment = new Adjustment();
    return $adjustment->newAdjustment($quote, $request)->adjustments;

On a side note if I run this in postman as raw JSON(applicatoin/json) it creates the resource in the DB:

{
"adjustments": [{
        "description": "testing-postman",
        "amount": 1000,
        "quote_id": 1
}
    ]

}

Upvotes: 0

Views: 72

Answers (1)

Denis Priebe
Denis Priebe

Reputation: 2855

As of Laravel 5.6, if you return a newly created model from your controller, Laravel will automatically set the response status to a 201. This typically is what you would do when building an API that follows RESTful practices.

However that may not suit your case as you may need to return other data from your controller and not just the newly created model and if so, I believe Laravel will return a 200 instead.

So you have a few of options:

  • In your controller you could force the 201 with return response($myData, 201);
  • Return only the newly created model and nothing else.
  • Or just have your test do the following:
    • $response->assertStatus(200);
    • $this->assertDatabaseHas('adjustments', $adjustment->toArray());

With the third option, your test is verifying that everything went okay and that the actual model was created and exists in the database (you'll need to adjust it based on your needs).

Upvotes: 3

Related Questions