Browser, Feature and Unit Testing in Laravel 5.7

Audun Rundberg
Jan 17 · 6 min read

Browser tests

<?phpnamespace Tests\Browser;use App\User;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
class ExampleCompanyTest extends DuskTestCase
{
public function testCreateCompany()
{
$user = User::find(1);
$this->browse(function (Browser $browser) use ($user) {
$browser->loginAs($user)
->visit('/companies/create')
->waitFor('@company-form')
->type('@name', 'Example company')
->type('@organization_number', '123456789')
->type('@bank_account', '1234 5678 9012')
->type('@net_days', '30')
->type('@address', 'Corporate Street 1')
->type('@phone', '+47 900 90 900')
->type('@email', 'hello@examplecompany.com')
->type('@website', 'examplecompany.com')
->click('@save-company')
->waitFor('@companies-table')
->assertPathIs('/companies')
->assertSee('Example company');
});
}
}
If the browser test fails, Laravel Dusk will save a screenshot of the failed state to the folder tests/Browser/screenshots
$browser->loginAs($user)
->visit('/companies/create')
->waitFor('@company-form')
->type('@name', 'Example company')
->type('@organization_number', '123456789')
->type('@bank_account', '1234 5678 9012')
->type('@net_days', '30')
->type('@address', 'Corporate Street 1')
->type('@phone', '+47 900 90 900')
->type('@email', 'hello@examplecompany.com')
->type('@website', 'examplecompany.com')
->click('@save-company')
    ->waitFor('@companies-table')
->assertPathIs('/companies')
->assertSee('Example company');

Feature tests

<?phpnamespace Tests\Feature;use App\Company;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ExampleCompanyTest extends TestCase
{
use RefreshDatabase;
public function testGetCompanyUnauthorized()
{
$user = factory(User::class)->create();
$company = factory(Company::class)->make();
$user->addCompany($company);
$anotherUser = factory(User::class)->create();
$response = $this
->actingAs($anotherUser, 'api')
->get('/api/v1/companies/'.$company->id);
$response->assertStatus(403);
}
}
$user = factory(User::class)->create();
$company = factory(Company::class)->make();
$user->addCompany($company);
$anotherUser = factory(User::class)->create();
$response = $this
->actingAs($anotherUser, 'api')
->get('/api/v1/companies/'.$company->id);
$response->assertStatus(403);

Unit tests

<?phpnamespace Tests\Unit;use App\Product;
use Tests\TestCase;
class ExampleProductTest extends TestCase
{
public function testProductWasBoughWithTax()
{
$product = factory(Product::class)->make([
'tax_rate' => 0.06,
]);
$this->assertTrue($product->was_bought_with_tax);
}
public function testProductWasNotBoughWithTax()
{
$product = factory(Product::class)->make([
'tax_rate' => 0,
]);
$this->assertFalse($product->was_bought_with_tax);
}
}
$product = factory(Product::class)->make([
'tax_rate' => 0.06,
]);
$this->assertTrue($product->was_bought_with_tax);
public function getWasBoughtWithTaxAttribute()
{
return (bool) ($this->tax_rate > 0);
}

Summary

Audun Rundberg

Written by

Content advisor at Netlife Design, Oslo

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade