![]() |
Place to write common methods containing tests - 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: Place to write common methods containing tests (/showthread.php?tid=86621) |
Place to write common methods containing tests - mzl_sadat - 02-05-2023 Hi there. Where should the methods that contain test statements and are shared between test controllers be written to run without problems? I could not call them correctly in a library in the support folder. It produces an error in setContext. thanks RE: Place to write common methods containing tests - InsiteFX - 02-06-2023 In order to take advantage of the additional tools provided, your tests must extend CIUnitTestCase. All tests are expected to be located in the tests/app directory by default. RE: Place to write common methods containing tests - mzl_sadat - 02-06-2023 thank you. So should I put them in a controller and call the required methods from other controllers? How do I call them? I tried this, but this controller is not found in other controllers. None of this works: 1- public function testOneMethod(){ $base= new Tests\App\Controllers\BaseTest(); $base->insertTestRecord(); } Or 2- public function testOneMethod(){ $base= new BaseTest(); $base->insertTestRecord(); } Error: Class "Tests\App\Controllers\BaseTest" not found RE: Place to write common methods containing tests - InsiteFX - 02-06-2023 To enable Controller Testing you need to use the ControllerTestTrait trait within your tests: CodeIgniter 4 Users Guide - Testing Controllers - The Helper Trait RE: Place to write common methods containing tests - mzl_sadat - 02-07-2023 My problem has not been solved yet, but thank you very much for your reply. RE: Place to write common methods containing tests - InsiteFX - 02-09-2023 All tests are expected to be located in the tests/app directory by default. So you should be able to place them in another folder. WATCH out for namespaces and the use causes. RE: Place to write common methods containing tests - kenjis - 02-11-2023 (02-05-2023, 10:01 AM)mzl_sadat Wrote: Where should the methods that contain test statements and are shared between test controllers be written to run without problems? In a base TestCase class for testing controllers. PHP Code: abstract class BaseControllerTestCase extends CIUnitTestCase PHP Code: class FooControllerTest extends BaseControllerTestCase RE: Place to write common methods containing tests - mzl_sadat - 02-12-2023 [quote pid="406709" dateline="1676168836"] Thank you. I have placed these two classes in the tests/app/Controllers directory as follows:
But when I run testOneMethod from MyTest class I get this result: PHP Fatal error: Uncaught Error: Class "Tests\App\Controllers\BaseControllerTestCase" not found in D:\xampp\htdocs\project\tests\app\Controllers\MyTest.php:5 I don't understand where the problem is. It does not matter whether this line is or not (in MyTest class): use Tests\App\Controllers\BaseControllerTestCase; Code: namespace CodeIgniter; [/quote] Code: namespace CodeIgniter; RE: Place to write common methods containing tests - kenjis - 02-12-2023 Tests\App\Controllers\BaseControllerTestCase Did you configure PSR-4 autoloader? It seems better you learn PHP's namespaces. https://www.w3schools.com/php/php_namespaces.asp |