laravel dusk assertSee failing
Hi, I am testing users cannot check out if someone check out first and there is no available book left. I am showing a message about not being available to checkout to that user. When I run test with assertSee it shows don't see that message but I can see that message in the failure screenshot so i added pause(1000) and waitForText() to test and run again. This time error is changed and it say Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constraint fails (SQL: drop table if exists `orders`) and doesn't migrate all tables to the database. If i remove pause() or waitForText(), it show assertSee error. I can't figure out what is happening.
here is my dusk test function
public function testCheckStockForCheckout()
{
$book = Book::factory()->create(['available_stock_count' => '1']);
User::factory(2)->create();
$this->browse(function ($first, $second) use ($book) {
$first->loginAs(User::find(1))
->visitRoute('books.index')
->screenshot('shop1')
->press('@addToCart')
->screenshot('addtocart1')
->visitRoute('cart.index')
->screenshot('cart1');
$second->loginAs(User::find(2))
->visitRoute('books.index')
->screenshot('shop2')
->press('@addToCart')
->pause(1000)
->screenshot('addtocart2')
->visitRoute('cart.index')
->screenshot('cart2')
->press('@checkout')
->pause(1000)
->assertPathIs('/checkout');
$first->press('@checkout')
->screenshot('checkout1')
// ->assertSeeIn('#overStockItemsMessage', "Some items in your cart are not available right now. $book->title");
// ->assertVisible('#overStockItemsMessage');
// ->assertSee("Some items in your cart are not available right now.
// $book->title");
->waitForText('Some items in your cart are not available right now.')
->assertSee('Some items in your cart are not available right now.');
});
}
cart.vue
<div
@click="checkStockForCheckout"
class="ml-5 bg-blue-500 px-3 py-1.5 rounded-md text-white shadow cursor-pointer"
dusk="checkout"
>
Checkout
</div>
checkStockForCheckout() {
// this should do with inertia and return inertia::render('Cart') with session
axios
.get('/carts/checkStockForCheckout')
.then(() => this.$inertia.visit('/checkout'))
.catch((err) => {
console.log(err.response.data.overstockitems)
this.overStockItems = err.response.data.overstockitems
})
},
cartcontroller
public function checkStockForCheckout()
{
if (auth()->check()) {
$overstockitems = Book::join('carts', 'books.id', '=', 'carts.book_id')
->where('carts.user_id', auth()->id())
// ->where('carts.quantity', '>', 'books.available_stock_count')
->whereRaw('carts.quantity > books.available_stock_count')
->get();
// foreach ($overstockitems as $cartItem) {
// Saveforlater::create([
// 'user_id' => auth()->id(),
// 'book_id' => $cartItem['id'],
// 'title' => $cartItem['title'],
// 'quantity' => $cartItem['quantity'],
// 'price' => $cartItem['price']
// ]);
// $cartItem->delete();
// }
} else {
$overstockitems = [];
foreach (session('cart') as $cartItem) {
$book = Book::where('id', $cartItem['id'])->first();
if ($cartItem['quantity'] > $book->available_stock_count) {
$overstockitems[] = [
'id' => $cartItem['id'],
'title' => $cartItem['title'],
'quantity' => $cartItem['quantity'],
'available_stock_count' => $book->available_stock_count,
];
// session()->put("saveforlater.{$cartItem['id']}", [
// 'id' => $cartItem['id'],
// 'title' => $cartItem['title'],
// 'quantity' => $cartItem['quantity'],
// 'price' => $cartItem['price'],
// ]);
// session()->pull("cart.{$cartItem['id']}");
}
}
}
if (count($overstockitems)) {
return response()->json([
'overstockitems' => $overstockitems
], 422);
}
return response()->json(['message' => 'ok']);
}
Does anyone know how to solve it? It is so bad. The test doesn't see the text but I can see it with my eyes in screenshot.
Please or to participate in this conversation.