Problem with testing validation |
06-07-2023, 05:24 AM
(This post was last modified: 06-07-2023, 08:04 AM by massimiliano1.mancini.)
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
Try this:
Code: --- a/system/Test/FeatureTestTrait.php
06-07-2023, 11:26 PM
(This post was last modified: 06-07-2023, 11:29 PM by massimiliano1.mancini.)
I tried and it works perfectly.
Thank you (06-07-2023, 02:38 PM)kenjis Wrote: Try this: |
Welcome Guest, Not a member yet? Register Sign In |