Try using dd() to see where you go in the code. Does it go into the catch perhaps?
Jul 28, 2022
11
Level 1
Database confirmation in my test fails
Hello guys!!!
I have a problem that I can not understand very well xq is failing me, I comment ......
I am doing a test with the following use case:
- An employee can return a tool
- And that return has to be approved.
In postman when I approve the return request, in the returns table it changes its status to => approved
but when I test and want to do an assrertDataBaseHas status => 1
it doesn't change the status o.O I share some code with you. Thanks in advance!
Test
/** @test */
public function accept_an_work_tool_return()
{
$this->return_working_tool();
$headers = $this->login_for_tests();
$route = $this->routes['approve'];
$approve_request = $this->json('PUT', $route . 1, [], $headers);
$approve_request->assertStatus(Response::HTTP_OK)
->assertSee(['id' => 1, 'status' => 1])
->assertJsonCount(1, 'result');
$this->assertDatabaseHas('emp_work_tool_returns', ['status' => 1]);
}
approve function
public function approveWorkToolReturn(int $id)
{
DB::beginTransaction();
try {
$tool = WorkToolReturn::findOrFail($id)->get();
$tool->each(fn ($value, $key) => $value->update(['status' => WorkToolReturn::$APPROVAL]));
DB::commit();
} catch (Exception $ex) {
DB::rollBack();
report($ex);
}
return $tool;
}
Level 102
@Alejo Huh? If it enters the catch, you should check what error is thrown
public function approveWorkToolReturn(int $id)
{
DB::beginTransaction();
try {
$tool = WorkToolReturn::findOrFail($id)->get();
$tool->each(fn ($value, $key) => $value->update(['status' => WorkToolReturn::$APPROVAL]));
DB::commit();
} catch (Exception $ex) {
DB::rollBack();
dd($ex->getMessage()); //get the message
report($ex);
}
return $tool;
}
1 like
Please or to participate in this conversation.