Jul 23, 2024
0
Level 3
Mockery persisting through different Pest Test
In my Pest tests i have two different tests on the same file: one calls a Mock on a class, the other is without Mock, to test if the correct Exception is thrown.
However, when the Mock Test is executed before the "Unmocked" test. The mock persists on the second test and that test fails.
So the test results depend on which order the tests are executed. 50% chance of success basically.
Any idea on how to solve this? other than putting the tests on two different pages?
//SequenceTest.php
// Unmocked test
it('throws error on missing class', function () {
$contact = Contact::factory()->create();
$sequence = Sequence::factory()->create(['name' => 'test']);
$sequence->start($contact);
})->throws(MissingClassException::class);
// Mocked test
it('throws error on missing start method', function () {
\Mockery::mock('alias:App\Teavel\Sequences\Test');
$contact = Contact::factory()->create();
$sequence = Sequence::factory()->create(['name' => 'test']);
$sequence->start($contact);
\Mockery::close();
})->throws(BadMethodCallException::class);
The $sequence->start(...) looks if the Sequence class exists.
public function start(Contact $contact){
if (! class_exists($className)) {
throw new MissingClassException("Sequence $Name class not found!");
}
try {
$className::start($contact);
} catch (\BadMethodCallException $e) {
throw new BadMethodCallException("Sequence $Name does not have a start method!");
}
}
Please or to participate in this conversation.