![]() |
Problem with testing validation - 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: Problem with testing validation (/showthread.php?tid=87844) |
Problem with testing validation - massimiliano1.mancini - 06-07-2023 I have a bunch of HTTP Feature tests. Some of that simply test that calling a post with correct fields a row in DB is added. For example: PHP Code: public function testInsertNewSectionCreateRowInDb() others test that invalid input do not add a new row in DB. For example (description is required): PHP Code: public function testInsertNewEmptySectionDoNotCreateRowInDb() Now, if I test in this order, i.e. tests with no validation errors before and tests with validation errors after, everithing is fine but if I run in the opposite order than all tests fail. I tried to investigate the cause and I think it depends on a shared validation service whom error array maintain last values. In particular in BaseService.php I found that when recall a static istance it comes back with array error not cleared PHP Code: protected static function getSharedInstance(string $key, ...$params) Running tests in a predefined order is not a good practice and so I'm asking for some advice and help on how to solve. EDIT: I'm not sure if this could be the right solution (or simply it skips all validations), but it works fine if this line PHP Code: Services::injectMock('validation', Services::validation(null, false)); Thank you Massimiliano RE: Problem with testing validation - kenjis - 06-07-2023 Try this: Code: --- a/system/Test/FeatureTestTrait.php RE: Problem with testing validation - kenjis - 06-07-2023 I sent a PR to fix: https://github.com/codeigniter4/CodeIgniter4/pull/7548 RE: Problem with testing validation - massimiliano1.mancini - 06-07-2023 I tried and it works perfectly. Thank you (06-07-2023, 02:38 PM)kenjis Wrote: Try this: |