I'm writing tests for Laravel and PHPUnit. My tests make use of a setUp method which runs factory methods when applicable. For example:
public function setUp()
{
parent::setUp();
factory(App\User::class, 10)->create();
}
As you can see, I have implicitly called parent::setUp().
However, when I use my own setUp method, traits do not work! For example, If I write a test which doesnt use a setUp() method, and I use the DatabaseTransactions trait, then everything works as expected. Transactions are working fine for this test.
However, If I add my own setUp() method, even if I run parent::setUp(), the trait doesn't work! I know it's being run, because if I var_dump() within the trait method, it outputs data, but it just doesn't run transactions!
If I remove my own setUp() method, transactions work as expected again!
It's not just the DatabaseTransactions trait, this behaviour is also displayed using the DatabaseMigrations trait. I haven't checked other traits.
However if I change my setUp() method to:
public function setUp()
{
parent::setUp();
$this->beginDatabaseTransaction();
$this->users = factory(App\User::class, 10)->create();
}
(note I have implicitly called the beginDatabaseTransaction() method) Then transactions will work.
Can anyone else explain if I am doing something wrong? Why do I need to implicitly call the trait methods if I override the setUp method?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire