CodeIgniter Forums
Testing controller method - 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: Testing controller method (/showthread.php?tid=80799)



Testing controller method - jojo - 12-19-2021

Hi guys,

I am making some unit tests on my app, and i couldn't find how to test methods of the controllers.

I added some methods in the BaseController and i want to check the returned value of some of my methods.

I don't really need to call any specific uri.

Can anyone help me please?

Thanks.


RE: Testing controller method - jojo - 12-20-2021

Just more details, i need to test the controller with a specific url query


RE: Testing controller method - kenjis - 12-20-2021

Create an object of BaseController,
and run a method you want to test,
and check the result.


RE: Testing controller method - jojo - 12-20-2021

@kenjis , thank you
I did it in that way but I couldn’t get the url query
I could set the body but not the query


RE: Testing controller method - kenjis - 12-20-2021

You have to set URI by your self.
But it is a bit complicated task.

Why don't you use withURI() in ControllerTestTrait?
https://codeigniter4.github.io/CodeIgniter4/testing/controllers.html#helper-methods


RE: Testing controller method - jojo - 12-20-2021

"kenjis", i have tried with that way too, but i don't know how to access to the controller methods?


RE: Testing controller method - kilishan - 12-20-2021

CodeIgniter has built in controller testing capability that should be just what you need. This will test the controller itself only, skipping the HTTP layer, the controller filters, etc, but if you wanted to test the whole stack, we do have tools for full HTTP testing also.


RE: Testing controller method - jojo - 12-20-2021

@kilishan, in need to test a returned value of one of the method of the controller. And just need a specific to set a specific query param in the url.
I have tried to test the controller with the the controller trait, or just make an instance of the controller if a new custom request in the initController method.
I can get the body of the request correctly (with $this->request->getJSON()), but not the url query (with $this->request->getGet()) even if i set it in the uri object of the request.
My function works in the application but looks like i cannot get by testing.