Is there a way to mock a class and use regular class construction?

Background: I am still learning to use mocks and am trying to test a WordPress plugin. I would prefer to not load WordPress and simply use mocks to fake class/function where needed and only test my code’s inputs and outputs.

I am trying to do the following:

Read More
// WP_Query IS NOT DEFINED

$mock = Mockery::mock('WP_Query', array('have_posts' => true));

$this->assertTrue($mock->have_posts());

$q = new WP_Query();

// fails with "Call to undefined method WP_Query::have_posts()"
$this->assertTrue($q->have_posts());

Is the above possible with Mockery?

Related posts

Leave a Reply

1 comment

  1. When passed an array as the second argument to Mockery::mock, it’s expecting constructor arguments, not the methods to be mocked.

    Instead, you need:

    $mock = Mockery::mock('WP_Query');
    $mock->shouldReceive('have_posts')->andReturn(true);