CodeIgniter Forums
How can I use a helper in a unit test? - 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: How can I use a helper in a unit test? (/showthread.php?tid=75004)



How can I use a helper in a unit test? - SteeveDroz - 12-06-2019

Hi there!

Here is my problem:

  1. I try to create a test case that extends CIDatabaseTestCase.
  2. In this one, I use a model of mine to access the database.
  3. In that model, I use the helper function "character_limiter()".
When running the test, PHPUnit tells me:

Code:
Error: Call to undefined function character_limiter()

I tried pretty much everything to allow the tests to recognize that function, even putting that ugly line at the beginning of my test case:

PHP Code:
require_once(ROOTPATH '/vendor/codeigniter4/framework/system/Helpers/text_helper.php'); 

Unfortunately, even that doesn't work, for ROOTPATH isn't defined by PHPUnit (obviously, silly me).

What is the best practice to test a model (or anything else for that mater) that uses a helper function? Do I need to modifiy my test or my code? And how?

Thanks in advance for your kind answers!


RE: How can I use a helper in a unit test? - ciadmin - 12-06-2019

Instead of require_once..., use helper('text');
See https://codeigniter4.github.io/userguide/helpers/text_helper.html


RE: How can I use a helper in a unit test? - SteeveDroz - 12-07-2019

I'm a bit ashamed it was that simple. I loaded the helper in my BaseController, I should have done the same in my test case!

Thanks for the answer!