0

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

So I am using PEST as my testing framework, and if I just define my mocks in the helpers, they won't be called correctly, because the WPCLI will probably be autoloaded after that on each test. So I added

beforeEach(function () {
    $wpCliMock = \Mockery::mock('alias:WP_CLI');

    $wpCliMock
        ->shouldReceive('success')
        ->andReturnArg(0);

    $wpCliMock
        ->shouldReceive('error')
        ->andReturnArg(0);
});

Inside my tests, and now everything is passing correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.