dryb38654 2019-04-30 19:25
浏览 51

Laravel测试响应给定数据无效

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;  
}
  • 写回答

1条回答 默认 最新

  • doubiaokai4998 2019-04-30 20:57
    关注

    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)

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!