To solve the issue you're encountering, you need to ensure that your custom AssertableInertiaModular class is being used instead of the default AssertableInertia class. The error you're seeing indicates that the assertInertia method is still using the default AssertableInertia class.
Here's a step-by-step solution to address this:
-
Create a Custom Test Response Macro: You need to override the
assertInertiamacro to use your customAssertableInertiaModularclass. -
Register the Custom Macro: Ensure that your custom macro is registered in your test setup.
-
Modify Your Test: Use the custom macro in your test.
Step 1: Create a Custom Test Response Macro
First, create a custom macro that uses your AssertableInertiaModular class. You can place this in a service provider or a dedicated test setup file.
use Illuminate\Testing\TestResponse;
use Modules\Main\Testing\AssertableInertiaModular;
TestResponse::macro('assertInertiaModular', function (Closure $callback) {
$assert = new AssertableInertiaModular($this);
$callback($assert);
return $this;
});
Step 2: Register the Custom Macro
Ensure that this macro is registered before your tests run. You can do this in a service provider or in the setUp method of your test case.
For example, if you want to register it in a service provider:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Testing\TestResponse;
class TestServiceProvider extends ServiceProvider
{
public function boot()
{
TestResponse::macro('assertInertiaModular', function (Closure $callback) {
$assert = new AssertableInertiaModular($this);
$callback($assert);
return $this;
});
}
}
Make sure to register this service provider in your config/app.php under the providers array.
Step 3: Modify Your Test
Now, modify your test to use the new assertInertiaModular macro.
public function testExample()
{
$response = $this->get('/admin');
$response->assertInertiaModular(function (AssertableInertiaModular $page) {
$page->component('Admin::index');
});
}
Full Example
Here is the full example with all the pieces put together:
Custom Macro Registration (e.g., in a service provider)
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Testing\TestResponse;
use Modules\Main\Testing\AssertableInertiaModular;
class TestServiceProvider extends ServiceProvider
{
public function boot()
{
TestResponse::macro('assertInertiaModular', function (Closure $callback) {
$assert = new AssertableInertiaModular($this);
$callback($assert);
return $this;
});
}
}
Custom AssertableInertiaModular Class
Ensure your AssertableInertiaModular class is correctly defined and extends AssertableInertia.
namespace Modules\Main\Testing;
use Inertia\Testing\AssertableInertia;
class AssertableInertiaModular extends AssertableInertia
{
public function component(string $value = null, $shouldExist = null): self
{
PHPUnit::assertSame($value, $this->component, 'Unexpected Inertia page component.');
if ($shouldExist || (is_null($shouldExist) && config('inertia.testing.ensure_pages_exist', true))) {
try {
$parts = explode('::', $value);
if (count($parts) > 1) {
// The component name is modular
$module = $parts[0];
$component = $parts[1];
app('inertia.testing.view-finder')->find($module . '/' . $component);
} else {
app('inertia.testing.view-finder')->find($value);
}
} catch (InvalidArgumentException $exception) {
PHPUnit::fail(sprintf('Inertia page component file [%s] does not exist.', $value));
}
}
return $this;
}
}
Test Case
public function testExample()
{
$response = $this->get('/admin');
$response->assertInertiaModular(function (AssertableInertiaModular $page) {
$page->component('Admin::index');
});
}
By following these steps, you should be able to use your custom AssertableInertiaModular class in your tests without encountering the type error.