Welcome Guest, Not a member yet? Register   Sign In
Problems with Unit Testing - HTTP Feature Testing - CI4
#1

Hello, i'm trying to make a simple test using 'HTTP Feature testing' as described in Documentation. But, after hours of trying I couldn't achieve it.

This is my implementation class:

PHP Code:
<?php

namespace App\Controllers;

//use App\Libraries\Traits\ExtendedController;
use CodeIgniter\Controller;

class 
Home extends Controller
{
 
  // use ExtendedController;

 
   public function index()
 
   {
 
       return view('welcome_message');
 
   }



And this is my Test class:
PHP Code:
<?php
namespace Tests;

use 
CodeIgniter\Test\FeatureTestCase;

class 
TestHome extends FeatureTestCase
{

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

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

 
   public function testIndex()
 
   {
 
       $result $this->call('get'site_url());
 
       $this->assertSee('Welcome to CodeIgniter');
 
   }


And this is the error thrown:
Code:
There was 1 error:

1) Tests\TestHome::testIndex
CodeIgniter\Exceptions\PageNotFoundException: Controller or its method is not found: App\Controllers\\Http:::ci4.extctrl.docker:8080

/var/www/html/ci4-extendedcontroller/vendor/codeigniter4/framework/system/Exceptions/PageNotFoundException.php:14
/var/www/html/ci4-extendedcontroller/vendor/codeigniter4/framework/system/CodeIgniter.php:889
/var/www/html/ci4-extendedcontroller/vendor/codeigniter4/framework/system/CodeIgniter.php:249
/var/www/html/ci4-extendedcontroller/vendor/codeigniter4/framework/system/Test/FeatureTestCase.php:122
/var/www/html/ci4-extendedcontroller/tests/TestHome.php:21

In my phpunit.xml, i've defined my env app.baseUrl as:    
Code:
<php>
       <env name="app.baseURL" value="http://ci4.extctrl.docker:8080/"/>
   </php>
Reply
#2

Your testcase should be in the App namespace. Not sure if that will solve the problem, but it's a start.
Reply
#3

(This post was last modified: 02-08-2019, 10:09 AM by CiUser.)

(02-08-2019, 08:53 AM)ciadmin Wrote: Your testcase should be in the App namespace. Not sure if that will solve the problem, but it's a start.

Okay, but, in documentation seems to indicate that the namespace should be 'Tests',isn't it?
I consider that if my testcase is inside /tests make sense that namespace be 'Tests' rather than 'App'

Though in HTTP Testing section indicates that namespace should be 'App'.

I'm a bit confused with that Undecided
Reply
#4

Ah - I see the contradiction. I think the testing writeup is incorrect. The tests/ folder just isolates test code from "real" code. I will experiment, & adjust the docs if needed.

My expectation would be:

<? php namespace App;
class MyAppTest extends \CIUnitTestCase {...}
Reply
#5

(This post was last modified: 02-15-2019, 08:31 AM by CiUser.)

(02-08-2019, 10:35 AM)ciadmin Wrote: Ah - I see the contradiction. I think the testing writeup is incorrect. The tests/ folder just isolates test code from "real" code. I will experiment, & adjust the docs if needed.

My expectation would be:

  <? php namespace App;
  class MyAppTest extends \CIUnitTestCase {...}

After debug the code I got the core of the error. For some reason class_exists not found 'App\Controllers\\Home' because of the first backslash in the namespace (App\Controllers) should be escaped as the second one (Controllers\\Home). So, the controller is not found.
Is there some config that I'm forgetting to to set or what could be the problem with that behaviour?

   
Reply




Theme © iAndrew 2016 - Forum software by © MyBB