Unit test for Many to Many Relationship

Posted 2 months ago by untymage

I'm trying to make sure that my many to many relationship will actually work but what is this error mean?

Failed asserting that Illuminate\Database\Eloquent\Collection Object (...) is an instance of class "App\Role".

My unit test:

$role = factory('App\Role')->create();

$this->assertInstanceOf(User::class, $role->users);



public function roles()
    return $this->belongsToMany(Role::class);


public function users()
    return $this->belongsToMany(User::class);

my pivot table:

Schema::create('role_user', function (Blueprint $table) {
        $table->primary(['role_id', 'user_id']);


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