Testing controller method |
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.
Create an object of BaseController,
and run a method you want to test, and check the result.
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/CodeIgnit...er-methods
"kenjis", i have tried with that way too, but i don't know how to access to the controller methods?
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.
@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. |
Welcome Guest, Not a member yet? Register Sign In |