Alejo's avatar
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:

  1. An employee can return a tool
  2. 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;
    }
0 likes
11 replies
Sinnbeck's avatar

Try using dd() to see where you go in the code. Does it go into the catch perhaps?

Alejo's avatar
Level 1

Hi @Sinnbeck, yes it goes into the try catch but just after the approval of the assignment, it doesn't print anything when I do ==> dd($tool) ..... why does this happen? any idea ..... thanks!

Sinnbeck's avatar

@Alejo Sorry I am confused. You say it goes into both try AND catch? So it fails?

Alejo's avatar
Level 1

@Sinnbeck, if it enters, but does not change the status to approved in the database, the status result is still zero, the catch jumps because I tell it in the assertion that in the database the state has to be one

Sinnbeck's avatar
Sinnbeck
Best Answer
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
Alejo's avatar
Level 1

@Sinnbeck, ahh okay! sorry for not explaining myself better.... this is the error I'm getting --> "date-time-error". o.O

Alejo's avatar
Level 1

@Sinnbeck, I remove the call to getMessage() and it prints this err.... I'm going to check the helper to see why it's returning err... Thanks for the help!!!!

#message: "date-time-error"
  #code: 400
  #file: "./app/Helpers/DateHelper.php"
  #line: 609

Please or to participate in this conversation.