I want to test my updating functionality. I can update a product on browser but I couldn't be able to pass the test.
Here my update method:
public function update(Request $request, Product $product)
{
if ($request->hasFile('image')) {
Storage::delete($product->image_path);
$product->update(['image_path' => request()->file('image')->store(auth()->id().'images', 'public')]);
}
$product->update(request()->validate([
'name' => 'required|string',
'description' => 'required|max:255',
'price' => 'required|numeric',
'compare_price' => 'required|numeric',
'charge_tax' => 'boolean',
'sku' =>[
'required',
'numeric',
Rule::unique('products')->ignore($product->id),
],
'inventory' => 'required|numeric',
'track_inventory' => 'required|boolean',
'width' => 'required|numeric',
'height' => 'required|numeric',
'depth' => 'required|numeric',
'weight' => 'required|numeric',
'weight_type' => 'required',
'extra_shipping_fee' => 'required|numeric',
]));
return redirect('/products');
}
Here my route:
Route::patch('/products/{product}', 'ProductsController@update');
Here my test:
/** @test * */
public function a_product_can_be_updated()
{
$this->signIn();
$product = create('App\Product', ['user_id' => auth()->id()]);
$this->patch($product->path(), // how to pass all data in a short way?
[
'name' => 'changed name',
'sku' => 32123,
]]);
tap($product->fresh(), function ($product) {
$this->assertEquals(32123, $product->sku);
$this->assertEquals('changed name', $product->name);
});
}
I know I must pass all data, but how can I make it in a clean way ? Can you show me an example please?
Aucun commentaire:
Enregistrer un commentaire