Nova requests & assertions for Laravel tests - View examples
Assert: Policies | Cards | Actions | Filters | Lenses | Resources | Fields | Relations
composer require dillingham/nova-assertions --dev
Enable by adding the NovaAssertions
to a test
use NovaTesting\NovaAssertions;
class UserTest extends TestCase
{
use NovaAssertions;
}
Log in a user that has access to Nova
$this->be(factory(User::class)->create());
Request using a resource's uriKey to perform assertions:
$response = $this->novaIndex('users');
$response = $this->novaDetail('users', $user->id);
$response = $this->novaCreate('users');
$response = $this->novaEdit('users', $user->id);
$response = $this->novaLens('users', Lens::class);
You may also pass filters & their values to indexes & lenses
$response = $this->novaIndex('users', [
StatusFilter::class => 'active'
]);
$response = $this->novaLens('users', Lens::class, [
StatusFilter::class => 'active'
]);
You can call http response methods as usual:
$response->assertOk();
$response->assertResourceCount(3);
$response->assertResources(function($resources) {
return $resources->count() > 0;
});
$response->assertCardCount(5);
$response->assertCardsInclude(Card::class);
$response->assertCardsExclude(Card::class);
$response->assertCards(function($cards) {
return $cards->count() > 0;
});
$response->assertActionCount(5);
$response->assertActionsInclude(Action::class);
$response->assertActionsExclude(Action::class);
$response->assertActions(function($actions) {
return $actions->count() > 0;
});
$response->assertFilterCount(5);
$response->assertFiltersInclude(Filter::class);
$response->assertFiltersExclude(Filter::class);
$response->assertFilters(function($filters) {
return $filters->count() > 0;
});
$response->assertLensCount(5);
$response->assertLensesInclude(Lens::class);
$response->assertLensesExclude(Lens::class);
$response->assertLenses(function($lenses) {
return $lenses->count() > 0;
});
$response->assertFieldCount(5);
Assert a specific field exists
$response->assertFieldsInclude('id');
Assert a specific field contains a value
$response->assertFieldsInclude('id', $user->id);
Assert multiple fields exist
$response->assertFieldsInclude(['id', 'email']);
Assert multiple fields with specific values exist
$response->assertFieldsInclude(['id' => 1, 'email' => 'example']);
Assert multiple values for one field exist
$response->assertFieldsInclude('id', $users->pluck('id'));
Make assertions against a collection of fields
$response->assertFields(function($fields) {
return $fields->count() > 0;
});
Also exclude
works in all of these scenarios
$response->assertFieldsExclude(['id' => 1, 'email' => 'example']);
// App\Nova\Post
// BelongsTo::make('Category'),
$response = $this->novaCreate('posts');
$response->assertRelation('categories', function($categories) {
//
});
// App\Nova\Category
// HasMany::make('Posts'),
$response = $this->novaDetail('categories');
$response->assertRelation('posts', function($posts) {
//
});
Assert Nova's use of policies & the authed user:
$response->assertCanView();
$response->assertCanCreate();
$response->assertCanUpdate();
$response->assertCanDelete();
$response->assertCanForceDelete();
$response->assertCanRestore();
Also can assert cannot
for each:
$response->assertCannotView();
Hi 👋, Im Brian Dillingham, creator of this Nova package and others
Hope you find it useful. Feel free to reach out with feedback.
Follow me on twitter: @im_brian_d