Place to write common methods containing tests |
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
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. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
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
To enable Controller Testing you need to use the ControllerTestTrait trait within your tests:
CodeIgniter 4 Users Guide - Testing Controllers - The Helper Trait What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
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. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(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
[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;
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 |
Welcome Guest, Not a member yet? Register Sign In |