Testing Helpers in Laravel 5.4

in #art7 years ago

These are some helper methods and settings to make writing tests in Laravel easier, more clear and less verbose. We’re following along from the amazing Laracast episode.

We are going to add some helper functions in a file that load for our tests in development. To do that head into composer.json and add the following:

"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
},
"files": ["tests/utilities/functions.php"]
},

We then create tests/utilities/functions.php and folder in our Laravel 5.4 application and include a couple helper methods for creating records and making new records. The create command will persist the records to the database. The make command will make instances of the records but not persist to database. There are more details about our tests in the last post.

<?php
function create($class, $attributes = [])
{
return factory($class)->create($attributes);
}
function make($class, $attributes = [])
{
return factory($class)->make($attributes);
}

This file will only be loaded for development. Run the dump-autoload command to rerun our composer.json file. Then run phpunit command to make sure all of our tests still pass.

$ composer dump-autoload
$ phpunit

We can then refactor our test code from:

$thread = factory(‘App\Thread’)->make();

to:

$thread = make(‘App\Thread’);

and from:

$reply = factory('App\Reply')->create(['thread_id' => $this->thread->id]);

to:

$reply = create('App\Reply', ['thread_id' => $this->thread->id]);

In the last post we had the following line to create a user and set the current authentication session to that user. The line below signs a user in so we can test the authentication and security features for our application:

$this->actingAs(factory('App\User')->create());

instead of doing that we would like to do:

$this->signIn();

To make that happen add the following to tests/TestCase.php:

<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
 protected function signIn($user = null)
{
// use passed in user or create one
$user = $user ?: create('App\User');
$this->actingAs($user);
return $this;
}
}

This method adds the signIn functionality we need and makes writing tests easier and less verbose.

Turning on and off exception handling

There is an issue in Laravel 5.4 with toggling on and off the exception handling. Adam Wathan has a fix here, adding functions to run tests with and without exception handling.

That’s what I got! Do you use tests in Laravel? What issues have you encountered? Feedback much appreciated. Thank you



Posted from my blog with SteemPress : http://selfscroll.com/testing-helpers-in-laravel-5-4/
Sort:  

This user is on the @buildawhale blacklist for one or more of the following reasons:

  • Spam
  • Plagiarism
  • Scam or Fraud

Warning! This user is on my black list, likely as a known plagiarist, spammer or ID thief. Please be cautious with this post!
If you believe this is an error, please chat with us in the #cheetah-appeals channel in our discord.