jgravois's avatar

Failing Test and I am Lost

Am I doing something wrong here?

I can't figure out WHY this test is failing. (I still have SO MUCH to learn about testing)

/** @test */
    public function user_can_update_return_admin_notes () {
        $user = factory(User::class)->create();
        $rma = factory(ReturnsRequest::class)->create([
            'initiator_id' => 1,
            'company_name' => 'Fruit Loops',
            'description' => 'Fruity breakfast cereal'
        ]);

        $this->assertDatabaseHas('returns_requests', [
            'initiator_id' => 1,
            'company_name' => 'Fruit Loops',
            'description' => 'Fruity breakfast cereal'
        ]);

        $this->actingAs($user)
            ->patchJson('/api/returns/update_rma/' . $rma->id, [
                'admin_notes' => 'This is a note for this RMA.'
            ]);

        $this->assertDatabaseHas('returns_requests', [
            'initiator_id' => 1,
            'company_name' => 'Fruit Loops',
            'description' => 'Fruity breakfast cereal',
            'admin_notes' => 'This is a note for this RMA.'
        ]);
    } // end test

That api route hits this controller method:

public function update_rma($id)
    {
        $arrUpdate = [
            'status_id' => request('status_id'),
            'initiator_id' => request('initiator_id'),
            'rma_number' => request('rma_number'),
            'has_other_fee' => request('has_other_fee'),
            'restocking_fee' => request('restocking_fee'),
            'exposure' => request('exposure'),
            'credit_memo_amount' => request('credit_memo_amount'),
            'restocking_fee_amount' => request('restocking_fee_amount'),
            'other_fee_amount' => request('other_fee_amount'),
            'adjusted_amount' => request('adjusted_amount'),
            'repair_cost_quote' => request('repair_cost_quote'),
            'contact' => request('contact'),
            'email' => request('email'),
            'other_fee_description' => request('other_fee_description'),
            'responsible_for_shipping' => request('responsible_for_shipping'),
            'rma_date' => request('rma_date'),
            'admin_notes' => request('admin_notes'),
            'description' => request('description'),
            'rma_explanation' => request('rma_explanation'),
        ];

        $rma = ReturnsRequest::with('reasons')->whereId($id)->first();
        $rma->update($arrUpdate);

        return new ReturnsResource($rma);
    } // end function

but I get this in testing

There was 1 failure:

1) Tests\Feature\Http\Controllers\ReturnsControllerTest::user_can_update_return_admin_notes
Failed asserting that a row in the table [returns_requests] matches the attributes {
    "initiator_id": 1,
    "company_name": "Fruit Loops",
    "description": "Fruity breakfast cereal",
    "admin_notes": "This is a note for this RMA."
}.

Found: [
    {
        "id": "1",
        "status_id": "1",
        "initiator_id": "1",
        "rma_number": "830809",
        "has_other_fee": "0",
        "restocking_fee": "1",
        "exposure": "15.101828524",
        "credit_memo_amount": "7.468288",
        "restocking_fee_amount": "7.24514821",
        "other_fee_amount": "10.5147019",
        "adjusted_amount": "44206.24723654",
        "repair_cost_quote": "5221.10429",
        "contact": "Danika Mueller",
        "email": "[email protected]",
        "other_fee_description": "exercitationem",
        "responsible_for_shipping": "nihil",
        "rma_date": "2011-11-30",
        "admin_notes": "Quisquam nihil ex sed odit rerum dolor. Quo optio dolorem temporibus. Est nihil dolor consequatur totam facilis illum aut.",
        "description": "Fruity breakfast cereal",
        "rma_explanation": "Aut soluta laudantium et sunt numquam eligendi harum. At id beatae consequatur. Fuga vitae rerum delectus qui ea porro nesciunt fugiat.",
        "created_at": "2019-11-20 17:48:14",
        "updated_at": "2019-11-20 17:48:14"
    }
].

/Users/jgravois/code/my-uam/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php:25
/Users/jgravois/code/my-uam/tests/Feature/Http/Controllers/ReturnsControllerTest.php:35
0 likes
8 replies
Sinnbeck's avatar

How does the ReturnsRequest factory look? Also is that assert 1 or two that fails?

jgravois's avatar
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class ReturnsResource extends JsonResource
{
    public function toArray($request)
    {
        //return parent::toArray($request);

        $awb = ($this->airway_bill ? env('SPACES_URI') . 'RMA/' . $this->id . '/' . $this->airway_bill : null);
        $damage = ($this->damage_image ? env('SPACES_URI') . 'RMA/' . $this->id . '/' . $this->damage_image : null);
        $failure = ($this->failure_report ? env('SPACES_URI') . 'RMA/' . $this->id . '/' . $this->failure_report : null);
        $plate = ($this->data_plate ? env('SPACES_URI') . 'RMA/' . $this->id . '/' . $this->data_plate : null);
        $scrapImg = ($this->scrap_image ? env('SPACES_URI') . 'RMA/' . $this->id . '/' . $this->scrap_image : null);
        $scrapRpt = ($this->scrap_report ? env('SPACES_URI') . 'RMA/' . $this->id . '/' . $this->scrap_report : null);
        $shop = ($this->shop_report ? env('SPACES_URI') . 'RMA/' . $this->id . '/' . $this->shop_report : null);

        return [
            'id' => $this->id,
            'status_id' => $this->status_id,
            'rma_number' => $this->rma_number,
            'invoice_no' => $this->invoice_no,
            'invoice_date' => $this->invoice_date,
            'created_at' => ($this->created_at ? $this->created_at->format('Y-m-d') : null),
            'creation_date' => ($this->created_at ? $this->created_at->diffForHumans() : null),
            'token' => $this->token,
            'company_name' => $this->company_name,
            'contact' => $this->contact,
            'email' => $this->email,
            'salesperson' => $this->salesperson,
            'initiator' => ($this->initiator ? $this->initiator->lastName : ($this->former_initiator ? $this->former_initiator->name : null)),
            'initiator_id' => $this->initiator_id,
            'airway_bill' => $awb,
            'damage_image' => $damage,
            'failure_report' => $failure,
            'scrap_image' => $scrapImg,
            'shop_report' => $shop,
            'scrap_report' => $scrapRpt,
            'data_plate' => $plate,
            'admin_notes' => $this->admin_notes,
            'airframe' => $this->airframe,
            'approver' => ($this->approver ? $this->approver->lastName : ($this->former_approver ? $this->former_approver->name : null)),
            'approve_id' => $this->approve_id,
            'approve_date' => $this->approve_date,
            'company_code' => $this->company_code,
            'condition_code' => $this->condition_code,
            'credit' => $this->credit,
            'ctrl_id' => $this->ctrl_id,
            'ctrl_number' => $this->ctrl_number,
            'consignment_code' => $this->consignment_code,
            'customer_po' => $this->customer_po,
            'description' => $this->description,
            'disposition_id' => $this->disposition_id,
            'exposure' => $this->exposure,
            'freehand_response' => $this->freehand_response,
            'has_other_fee' => $this->has_other_fee,
            'is_expired' => $this->is_expired,
            'credit_memo_amount' => $this->credit_memo_amount,
            'item_number' => $this->item_number,
            'other_fee_description' => $this->other_fee_description,
            'part_number' => $this->part_number,
            'post_date' => $this->post_date,
            'price' => $this->price,
            'cost' => $this->cost,
            'credit_adjusted' => $this->credit_adjusted,
            'credit_full' => $this->credit_full,
            'restocking_fee_amount' => $this->restocking_fee_amount,
            'other_fee_amount' => $this->other_fee_amount,
            'adjusted_amount' => $this->adjusted_amount,
            'restocking_fee' => $this->restocking_fee,
            'receive_date' => $this->receive_date,
            'receiver' => ($this->receiver ? $this->receiver->lastName : ($this->former_receiver ? $this->former_receiver->name : null)),
            'receive_id' => $this->receive_id,
            'repair_cost_quote' => $this->repair_cost_quote,
            'request_date' => $this->request_date,
            'return_deadline' => $this->return_deadline,
            'return_override_reason' => $this->return_override_reason,
            'return_reason_id' => $this->return_reason_id,
            'rma_date' => $this->rma_date,
            'rma_explanation' => $this->rma_explanation,
            'serial_no' => $this->serial_no,
            'responsible_for_shipping' => $this->responsible_for_shipping,
            'shop_purchase_order' => $this->shop_purchase_order,
            'shop_used' => $this->shop_used,
            'stock_line' => $this->stock_line,
            'submit_deadline' => $this->submit_deadline,
            'submit_override_reason' => $this->submit_override_reason,
            'verifier' => ($this->verifier ? $this->verifier->lastName : ($this->former_verifier ? $this->former_verifier->name : null)),
            'verify_id' => $this->verify_id,
            'verify_date' => $this->verify_date,
            'reasons' => collect($this->reasons)->pluck('id'),
            'causes' => collect($this->reasons)->pluck('name')
        ];
    }
}
jgravois's avatar

I have additional fields in there that I edited out of the initial question for brevity

jgravois's avatar

not that the original question is brief ... LOL

Talinon's avatar
Talinon
Best Answer
Level 51

@jgravois

Well, first of all, it looks like you don't have a company_name field within your returns_requests table, so the test will fail against that.

As for the admin_notes not being updated, are you certain it's actually hitting that method on your controller? Could middleware be intercepting it? Add a dd() and make sure the method is actually being called, or inspect the response:


dd($this->actingAs($user)
            ->patchJson('/api/returns/update_rma/' . $rma->id, [
                'admin_notes' => 'This is a note for this RMA.'
            ]));

jgravois's avatar

FAILURES! Tests: 1, Assertions: 2, Failures: 1.

jgravois's avatar

the field does exist in table .... I removed several field from the original question

dumping now

jgravois's avatar

dd() was the secret ... showed I had this

"message" => "SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: returns_requests.customer_was_notified (SQL: update "returns_requests" set "status_id" = ?, "initiator_id" = ?, "disposition_id" = ?, "receive_id" = ?, "return_reason_id" = ?, "rma_number" = ?, "customer_was_notified" = ?, "has_other_fee" = ?, "credit_adjusted" = ?, "credit_full" = ?, "restocking_fee" = ?, "exposure" = ?, "credit_memo_amount" = ?, "restocking_fee_amount" = ?, "other_fee_amount" = ?, "adjusted_amount" = ?, "repair_cost_quote" = ?, "contact" = ?, "email" = ?, "airway_bill" = ?, "damage_image" = ?, "failure_report" = ?, "scrap_image" = ?, "scrap_report" = ?, "shop_report" = ?, "data_plate" = ?, "airframe" = ?, "credit" = ?, "other_fee_description" = ?, "shop_purchase_order" = ?, "shop_used" = ?, "responsible_for_shipping" = ?, "approve_id" = ?, "verify_id" = ?, "approve_date" = ?, "invoice_date" = ?, "post_date" = ?, "receive_date" = ?, "request_date" = ?, "return_deadline" = ?, "rma_date" = ?, "submit_deadline" = ?, "verify_date" = ?, "admin_notes" = This is a note for this RMA., "description" = ?, "freehand_response" = ?, "rma_explanation" = ?, "updated_at" = 2019-11-20 18:09:10 where "id" = 1)

1 like

Please or to participate in this conversation.