Instead of use PHPUnit\Framework\TestCase; I used use Tests\TestCase;
Nov 12, 2022
3
Level 9
Illuminate\Contracts\Container\BindingResolutionException Target class [config] does not exist.
I'm getting this error when I try to run a PHPUnit test that use model factories
use App\Models\Post;
use App\Models\Channel;
class ChannelTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function a_channel_has_many_posts()
{
//$user ChannelCategory::factory()->create()->id;
$channel = Channel::factory()->create();
$post = Post::factory()->create(['channel_id' => $channel->id]);
$this->assertTrue($channel->posts->contains($post));
}
}
I've read in other threads to setup and teardown in the TestCase:
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
public function setUp(): void
{
parent::setUp();
}
public function tearDown(): void
{
parent::tearDown();
}
}
I've also php artisan config:clear and php artisan cache:clear with no luck. I did have to change the Tests name from Channel to ChannelTest not sure if that did something.
After doing all of this I'm still getting:
• Tests\Unit\ChannelTest > a channel has many posts
Illuminate\Contracts\Container\BindingResolutionException
Target class [config] does not exist.
at vendor/laravel/framework/src/Illuminate/Container/Container.php:877
873▕
874▕ try {
875▕ $reflector = new ReflectionClass($concrete);
876▕ } catch (ReflectionException $e) {
➜ 877▕ throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
878▕ }
879▕
880▕ // If the type is not instantiable, the developer is attempting to resolve
881▕ // an abstract type such as an Interface or Abstract Class and there is
+6 vendor frames
7 database/factories/ChannelFactory.php:21
fake()
+7 vendor frames
15 tests/Unit/ChannelTest.php:30
Illuminate\Database\Eloquent\Factories\Factory::create()
Level 9
12 likes
Please or to participate in this conversation.