2

I'm just trying to implement unit testing to my PHP application and really can't figure out how it works with mock/stub and Codeception

So I'm using Laravel and want to add testing to an API endpoint like this

$I->sendGET('/user/inventory/<user_id>');

Then in the controller I have function to authenticate the user, something like

public function getUserInventory($userId) {

    if ($this->authenticateUser($userId)) 
    {
        do some logic...
        if ($success) {
            return $good_data;
        } else {
            return $bad_data;
        }
    } 
    return false;
}

authenticateUser function is not relevant to the test and I just want to mock the return to true or false so I can test the actual logic on whether it should return $good_data or $bad_data, is that possible?

I've tried to declare stub object before calling the sendGET function like this

Stub::make(
    'App\Http\Controllers\Controller',
    ['authenticateUser' => true],
    $this
);

$I->sendGET('/user/inventory/<user_id>');

but it doesn't seem to use the stub object inside the getUserInventory function when I ran the test.

Is there any way to use stub/mock method inside the $I->sendGET('/user/inventory/<user_id>'); test?

Hopefully my question make sense

morgan9999
  • 731
  • 1
  • 11
  • 30
  • Ideally there shouldn't be any mocks in functional tests and you would actually authenticate in tests. If you really have to mock something, you shouldn't mock a method of controller, but some dependency that you can inject using haveInstance, haveBinding or haveSingleton methods of Laravel5 module (I have no experience with Laravel testing) – Naktibalda Aug 09 '19 at 16:49
  • Thanks for the advice, I was kinda wondering if I'm doing the right thing at all. If it turns out to be impossible then it might not be the best way to test. As I mentioned, I just trying out unit testing and still figuring out best practice on how and what to test – morgan9999 Aug 09 '19 at 16:54
  • I never wrote unit tests for my controllers as I like them small. (The logic inside them is moved to other services, each of them having unit tests.) The controller would then be tested via functional tests. It appears that you are in the right testing domain (having functional tests as unit tests) -- that will increase your mocking pain and I'd advise against that. That being said: other tried that https://matthewdaly.co.uk/blog/2018/02/25/unit-testing-your-laravel-controllers/ – k0pernikus Aug 09 '19 at 17:03
  • I also remembering facing similar issues as you did when I was starting testing. My biggest pain point was conflating the difference between "unit tests" and "functional tests" as the same thing. See my musings here: https://stackoverflow.com/a/35463959/457268 even though that relates to symfony2 with a database-heavy stack, it may help you understanding. – k0pernikus Aug 09 '19 at 17:09
  • This may help understanding the difference testing types: https://codeutopia.net/blog/2015/04/11/what-are-unit-testing-integration-testing-and-functional-testing/ – k0pernikus Aug 09 '19 at 17:13
  • Hey, thanks for the reading material. Really helps me get a better understanding about unit testing – morgan9999 Aug 10 '19 at 07:38

0 Answers0