Laravel Packages

Test Views with Laravel Mojito

Published
Test Views with Laravel Mojito image

Laravel Mojito is a lightweight package for testing Laravel views in isolation. Here’s an example of basic usage from the readme:

class WelcomeTest extends TestCase
{
// First, add the `InteractsWithViews` trait to your test case class.
use InteractsWithViews;
 
public function testDisplaysLaravel()
{
// Then, get started with Mojito using the `assertView` method.
$this->assertView('welcome')->contains('Laravel');
}
}

You can also use this package in HTTP tests:

$response = $this->get('/');
 
$response->assertStatus(200);
 
$response->assertView()->contains('Laravel');

The API includes the following features at the time of writing:

  • contains() – assert the view has the given text
  • has() – assert the view has the given selector
  • hasAttribute() – assert an element has the given attribute value
  • hasClass() – assert the view has an element with the given class
  • hasLink() – assert the view has an element with the given link

Here’s some more examples of the package methods:

// contains
$this->assertView('button')->contains('Click me');
 
// has
$this->assertView('welcome')->in('body')->has('.content')
 
// hasAttribute
$this->assertView('button')->hasAttribute('attribute', 'value');
 
// hasClass
$this->assertView('button')->hasClass('btn');
 
// hasLink
$this->assertView('button')->hasLink(route('welcome'));

You can learn more about this package, get full installation instructions, and view the source code on GitHub at nunomaduro/laravel-mojito.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article