N
N
Nikita Dergachov2017-12-27 11:55:30
PHPUnit
Nikita Dergachov, 2017-12-27 11:55:30

How to execute methods before running tests?

I use dusk for testing. I need to execute a method before running the test. I tried calling the method in the constructor.

class browserTest extends DuskTestCase
{
    public function __construct($name = null, array $data = [], $dataName = '')
    {
        parent::__construct($name, $data, $dataName);
        $this->foo();
    }

    protected function foo()
    {
         //code
    }

But then it throws an error
PHP Fatal error:  Uncaught RuntimeException: A facade root has not been set. in vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:218

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Voll., 2017-12-27
@vanillathunder

Public function setUp() or you can move it to the base class and extend tests from it

A
Alexander Aksentiev, 2017-12-27
@Sanasol

DuskTestCase also has a special prepare method to run before tests.
Well, in general, to fence some kind of logic and it’s not clear why such calls in tests are such a solution.
From the preparation for the test, there should be nothing but connecting to the database and rolling the migration.
If you need to do something, then do it in the tests themselves. Why not call this method in the test?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question