Skip to content

Instantly share code, notes, and snippets.

@tonyfrenzy
Last active September 28, 2019 08:53
Show Gist options
  • Save tonyfrenzy/92ff0f9ddea4c91d4027f3528fa8c646 to your computer and use it in GitHub Desktop.
Save tonyfrenzy/92ff0f9ddea4c91d4027f3528fa8c646 to your computer and use it in GitHub Desktop.
belongsToMany() Relationship Test I (User-Role) - Testing Model Relationships in Laravel
<?php
namespace Tests\Unit;
use App\Role;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
class UserTest extends TestCase
{
use RefreshDatabase, WithFaker;
/** @test */
public function a_user_belongs_to_many_roles()
{
$user = factory(User::class)->create();
$role = factory(Role::class)->create();
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $user->roles);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment