Skip to content

Instantly share code, notes, and snippets.

@tonyfrenzy
Last active September 28, 2019 08:53
Show Gist options
  • Save tonyfrenzy/5ddc9bdf96d2355c3e306a781228fb26 to your computer and use it in GitHub Desktop.
Save tonyfrenzy/5ddc9bdf96d2355c3e306a781228fb26 to your computer and use it in GitHub Desktop.
belongsToMany() Relationship Test 2 (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 RolesTest extends TestCase
{
use RefreshDatabase, WithFaker;
/** @test */
public function roles_database_has_expected_columns()
{
$this->assertTrue(
Schema::hasColumns('roles', [
'id', 'title', 'description'
]), 1);
}
/** @test */
public function a_role_belongs_to_many_users()
{
$user = factory(User::class)->create();
$role = factory(Role::class)->create();
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $role->users);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment