Mike
Mike

Reputation: 107

Laravel test response with The given data was invalid

I'm doing unit test with laravel, so I called controller function and I get like a respnse an array
I have been response with this

return back()->with('success', 'Lots was generated')  

and

return $this->lots_available;  

The test give me as response this:

There was 1 error:

  1. Tests\Feature\LotTest::test_lots Illuminate\Validation\ValidationException: The given data was invalid.

I don't understand the reazon to this response, I'm beginning with the test

This is my function test

public function test_lots()  
{    
    $this->withoutExceptionHandling();  

    $product = factory(Product::class)->create([
        'size' => 20
    ]);

    $lots = factory(Lot::class, 10)->create([
        'product_id' => $product->id,
    ]);

    $admin = factory(User::class)->create([
        'role_id' => 3
    ]);

    $client_request = 500;

    $this->actingAs($admin)
    ->post(route('lots.distribution'), [$product, $client_request])
    ->assertStatus(200);
}  

And this my called method

public function distribute(ProductRequest $product, $client_order)
{
    $this->lots = $product->lots;  
    $this->client_order = $client_order;  
    $this->getLotAvailable();

    return $this->lots_available;  
}

Upvotes: 0

Views: 4492

Answers (2)

Felipe Medeiros
Felipe Medeiros

Reputation: 51

Put the response in a variable and use dd() to print it.

You will find it on the messages method. Worked for me.

dd($response);

Upvotes: 0

Clément Baconnier
Clément Baconnier

Reputation: 6108

Assuming your route is something like Route::post('/distribute/{product}/{client_order}')

route('lots.distribution') needs the parameters inside the function call

route('lots.distribution', [$product, $client_request])

Then you need to send the data that passes your rules in ProductRequest otherwise you will get a validation error. If you try a dd(session('errors')) after the post, you will probably see errors about missing fields.

->post(
    route('lots.distribution', [$product, $client_request]), 
    ['title => 'unique_title', 'sap_id' => 'unique_id']
)

Finally in your method, I'm assuming that the request ProductRequest is different than the Model Product:

public function distribute(ProductRequest $request, Product $product, $client_order)

Upvotes: 1

Related Questions