Welcome Guest, Not a member yet? Register   Sign In
Place to write common methods containing tests
#1

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
Reply
#2

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 )
Reply
#3

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
Reply
#4

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 )
Reply
#5

My problem has not been solved yet, but thank you very much for your reply.
Reply
#6

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 )
Reply
#7

(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
{

Reply
#8

[quote pid="406709" dateline="1676168836"]
Thank you.
I have placed these two classes in the tests/app/Controllers directory as follows:
  1. BaseControllerTestCase
  2. MyTest

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;

use CodeIgniter\Test\CIUnitTestCase;

abstract class BaseControllerTestCase extends CIUnitTestCase {

    protected function setUp(): void {
        parent::setUp();
    }

    protected function tearDown(): void {
        parent::tearDown();
    }

    public function myMethod(){
        print_r('-------ok---------');
    }
}

[/quote]
Code:
namespace CodeIgniter;
use Tests\App\Controllers\BaseControllerTestCase;


class MyTest extends BaseControllerTestCase {

    protected function setUp(): void {
        parent::setUp();
    }

    protected function tearDown(): void {
        parent::tearDown();
    }

    public function testOneMethod(){
        $this->myMethod();
    }
 
}
Reply
#9

(This post was last modified: 02-12-2023, 04:22 AM by kenjis.)

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB