connecteev
5 months ago
705
1
Testing

Factory error on calling $user->roles()->sync(1) from Unit Test

Posted 5 months ago by connecteev

I'm seeing a weird error when I try to create my first unit test to create a user and call sync()

class PaymentsAndSubscriptionsTest extends TestCase
{
    // use RefreshDatabase; // cleans out the entire database

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

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

    /**
     * Can do something
     * @test
     */
    public function can_do_something2()
    {
        $user = $this->create('User', [], false);
        $user->roles()->sync(1); // make sure the new user created has their 'role' set, otherwise we'll get a 403 unauthorized response
    }

Error:

phpunit
PHPUnit 8.5.3 by Sebastian Bergmann and contributors.

....S..EPHP Fatal error:  Cannot redeclare createUnique() (previously declared in /kb_backend_apis_laravel/database/factories/ReactionFactory.php:41) in /kb_backend_apis_laravel/database/factories/ReactionFactory.php on line 41
PHP Fatal error:  Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. in /kb_backend_apis_laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php:1011

this is the factory where it's coming from, except I'm not calling it from my unit test...

$factory->state(Reaction::class, 'to_posts', function (Faker $faker) {
    return createUnique($faker, "Post");
});

$factory->state(Reaction::class, 'to_comments', function (Faker $faker) {
    return createUnique($faker, "Comment");
});

// $entityType: Post | Comment
function createUnique(Faker $faker, $entityType)
{
...
}

Any ideas?

Please sign in or create an account to participate in this conversation.