Hi,
I'm using 4.5.4 and I'm trying to set up some tests but I'm having a difficult time. This is the code from my controller:
Code:
$data['is_new'] = $is_new;
return view('my_body', $data);
Then in the view I have this:
PHP Code:
<?php
?>
if ($is_new) {
<?php
print "Welcome, new user.";
?>
} else {
print "Welcome back.";
}
In my test, which I pretty much copied from the docs:
PHP Code:
<?php
namespace app\Controllers;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\FeatureTestTrait;
use CodeIgniter\Test\DatabaseTestTrait;
use PHPUnit\Framework\Attributes\TestDox;
class MyControllerFeaturesTest extends CIUnitTestCase
{
use FeatureTestTrait;
use DatabaseTestTrait;
/**
* setup and teardown run before and after
* the _entire_ test case
* setup and tearDown run _between_ tests
*/
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
}
public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
}
protected function setUp(): void
{
parent::setUp(); // Do not forget
helper('text');
}
protected function tearDown(): void {
parent::tearDown();
}
#[TestDox('A new user should get a welcome')]
public function testViewingResults(): void
{
$response = $this->get('/forum/loggedin/79435')
->call();
$response->assertSee('Welcome, new user');
}
}
The error I'm getting is that $is_new is not defined in the view:
Code:
ErrorException: Undefined variable $is_new
The page actually works just fine, no errors at all. I can go to the url, see the appropriate message based on whether or not I'm a new user.
Any ideas?