BIOS
BIOS

Reputation: 1695

Mockery - creating a mock in the constructor

Is there a way to create a mock in the test class constructor/class setUp function such that the mock is available to all test methods?

I have tried creating in the constructor like:

public class testMocks extends PHPUnit_Framework_TestCase {

    protected $mock;

    public function __construct()
    {
        $this->mock = Mockery::mock('myMockedClass');
    }

...

But this doesn't work. If the first test passes, then all tests that assert on the mock pass even if they should fail (i.e running a shouldReceive that should fail). Any ideas?

Upvotes: 0

Views: 701

Answers (2)

Bram Gerritsen
Bram Gerritsen

Reputation: 7238

You shouldn't overwrite the constructor of PHPUnit_Framework_TestCase, see my answer on #15051271 and also #17504870

You also need to call Mockery::close() on tearDown method. The close method cleans up the mockery container for your next test and runs the expectations you have setup.

public function tearDown()
{
    Mockery::close();
}

Upvotes: 0

Rodrigo Gauzmanf
Rodrigo Gauzmanf

Reputation: 2527

You have to use setUp function, like this:

public function setUp()    
{
    $this->mock = Mockery::mock('myMockedClass');
}

Upvotes: 1

Related Questions