Mocking WP_CLI static methods in unit tests

Question

I’m writing unit tests (not integration) for a lib I’m working on. There, we created some WP-CLI commands that I’d like to test. Most of them are just renaming things, copying and pasting things over from the lib to the project.

And when something is successful or not a WP_CLI::success() or similar methods are used to output the message to the user (log, warning or error).

So what I’d like to do is just to output the contents of those methods so that I can test if the desired output happened.

Using Mockery I tried doing the following

// Mock certain WPCLI methods.
$wpCliMock = Mockery::mock('alias:WP_CLI');
$wpCliMock
    ->shouldReceive('success')
    ->andReturnArg(0);

But it seems that it isn’t working because my tests throw Call to a member function fetchMock() on null and the error points to the success method.

The tests are located here. I’m using Pest as my testing framework.

0
dingo_d 8 months 2020-12-07T09:10:21-05:00 0 Answers 8 views 0

Leave an answer

Browse
Browse