Testing a controller that has validation: getVar don't work |
Hi everyone,
I'm trying to code a unit test for a controller that has validation but it seems that the method $request->getVar don't work when you set de request in the test: Code: <?php If you get the value from the request: PHP Code: var_dump($request->getVar('description')); The first one results: NULL and the second: string(15) "This is a test!", that is corret. The problem is that the validation uses the method getVar internally, resulting in a bad behavior in the test. Here the store function in the controller: PHP Code: // insert data As I said above, if I use getPost I can get the values, but the validation fails because uses getVar. I'm using the codeigniter version 4.2.11 and php 7.4. Thanks in advance!
I think using getPost() is better when you use POST requests.
Why do you need to use getVar()? Ah, $this->validate() uses getVar()! 1. use $this->validateData() https://codeigniter4.github.io/CodeIgnit...lidatedata 2. set 'request' in the test. PHP Code: $request->setGlobal('request', [
(03-01-2023, 12:30 AM)kenjis Wrote: I think using getPost() is better when you use POST requests. For now I setted 'request' in the test and it works! But I'll change all validations to use 'validateData' because seem to me the right way to solve this. Thanks! |
Welcome Guest, Not a member yet? Register Sign In |