Welcome Guest, Not a member yet? Register   Sign In
Unit Testing - FeatureTest
#1

Hi;

I'm new to CI4 Unit testing. I've tried to do a HTTP feature testing (following the CI4 documentation), no matter how I tweak my test code, the return always as below (PageNotFoundException) :
Quote:1) App\HomeTest::testIndexPageWithRecordFound
CodeIgniter\Exceptions\PageNotFoundException: Controller or its method is not found: \App\Controllers\Ci4_proj::public

D:\Projects\www\ci4_proj\vendor\codeigniter4\framework\system\Exceptions\PageNotFoundException.php:14
D:\Projects\www\ci4_proj\vendor\codeigniter4\framework\system\CodeIgniter.php:981
D:\Projects\www\ci4_proj\vendor\codeigniter4\framework\system\CodeIgniter.php:324
D:\Projects\www\ci4_proj\vendor\codeigniter4\framework\system\Test\FeatureTestCase.php:182
D:\Projects\www\ci4_proj\tests\app\HomeTest.php:18

My unit test code as below:

Code:
<?php namespace App;

use CodeIgniter\Test\FeatureTestCase;

class HomeTest extends FeatureTestCase {

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

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

    public function testIndexPageWithRecordFound() {
        $result = $this->call('get', 'http://localhost/ci4_proj/public');
        $this->assertTrue($result->isOK());
    }
}

I'm using CodeIgniter 4.0.3, installed via composer (codeigniter4/appstarter). My unit test file located in 'D:/projects/www/ci4_proj/tests/app'.

I've tried to search from the web about CI4 Unit Test, but no luck, and try to find from the forum, but cannot get the answer. Can somebody please help to shed the light or point out where is my mistake. I've been studied on CI4 unit test for several days and no progress at all....  Sad

Thanks.
Reply
#2

Hi;

I've managed to make it work by changing the below:

Code:
1) $result = $this->call('get', 'home/index');

2) Changed in the phpunit.xml - <server name="app.baseURL" value="http://localhost"/>

Seems like the $path parameter on the call() is expecting "Controller/Method" instead of site URL (like documentation sample). One more thing, if you're using '.env' file, the app.BaseURL from .env file will take precedence. Also found that leave the app.BaseURL to empty ("") will work as well. 

Hope this help!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB