CodeIgniter Forums
Mock models / controller methods in tests - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Mock models / controller methods in tests (/showthread.php?tid=77306)



Mock models / controller methods in tests - moutarde - 08-14-2020

Hey!

I switched a while back from CI3 to CI4 but wasn't able to migrate my tests as I can't find a way to mock my models or even QueryBuilder like I did on CI3 with kenjis/ci-phpunit-test.
Is there any way to do something like that on CI4?

PHP Code:
$mock $this->getDouble(
    'PreferenceModel',
    ['getPreference' => new Preference([])]
);

$this->request->setCallable(
    function ($CI) use ($mock) {
        $CI->PreferenceModel $mock;
    }
); 
This code would mock $this->PreferenceModel in the controller.

Plus, is there any way to mock controller methods?


RE: Mock models / controller methods in tests - moutarde - 08-31-2020

I'm still looking for a solution


RE: Mock models / controller methods in tests - paulbalandan - 08-31-2020

In what context are you using it? Inside a controller? You can use PHPUnit's mock methods.