lundi 28 octobre 2019

Write unit test for try catch in laravel 5.8

I have something like this in my controller:

public function method()
{
    try {
        $data = Model::method();

        return $data;
    } catch (Exception $e) {
        return $e->getMessage();
    }
}

while I'm looking for writing unit test for this method, in this way:

public function testMethod()
{
    $var = (new \App\Http\Controllers\MyController)->method();

    $this->assertTrue(true);
}

my question is when I run phpunit in code-coverage mode it returns that catch block in my controller code isn't covered.

first question is How should I do cover the catch block?

and my second question is that How I can say $var values equals to something which I define.

Aucun commentaire:

Enregistrer un commentaire