Before assertRedirect(), do $response->dump() and show output here.
Apr 24, 2021
13
Level 10
Failed asserting that two strings are equal.
I have a test that can test if an admin can edit products
and when I run it I get the error Failed asserting that two strings are equal. how can I fix this?
Test
public function test_admin_can_edit_products()
{
$this->withExceptionHandling();
$user = User::factory()->create(['admin' => 1]);
$product = Product::factory()->create();
$response = $this->actingAs($user)->patch('/admin/product/' . $product->slug, [
'name' => 'iPhone X',
'description' => 'This is a new iphone',
'price' => 220,
]);
$response->assertRedirect(route('product.index'));
}
my edit controller
public function update(Request $request, Product $product)
{
$validated = $request->validate([
'name' => 'required|unique:products,name,' . $product->id,
'slug' => 'required|unique:products,slug,' . $product->id,
'price' => 'required|integer',
'description' => 'required',
]);
$product->update($validated);
$this->calculateDiscount($product);
return redirect(route('product.index'));
}
private function calculateDiscount(Product $product)
{
if($product->price >= 112 && $product->price <= 115) {
$percentage = 0.25;
$price = $product->price;
$discount = ($percentage / 100) * $price;
if($product->discount()->exists()){
$product->discount()->update(['discount' => $discount]);
} else {
$product->discount()->create(['discount' => $discount]);
}
}
if($product->price >= 120) {
$percentage = 0.50;
$price = $product->price;
$discount = ($percentage / 100) * $price;
$product->discount()->create(['discount' => $discount]);
}
}
Level 27
I would put a dd("this is update") after the validation of your update(...) to be sure you are hitting it.
You are validating for 'slug', but I don't see it in your test.
Please or to participate in this conversation.