Testing your view based route in PHPUnit
Using the above recipe, I was able to make a view right from the route, now I want to test my view and make sure it is doing what is expected. Typically, I would use Behat but in this example, I will use PHPUnit.
Getting ready
If you follow the above recipe, you will have an example page in place. From here we will start with the testing.
How to do it…
Follow the listed steps for testing your view based route:
- As usual, make a test:
>php artisan make:test ExampleViewTest
- Then, in that file
tests/ExampleViewTest.php
, I begin to write a test: - Run the test and it passes:
> phpunit --filter=example_view
- Now, to see how we can deal with authentication, add this to the route:
- And what was a passing test is now failing:
- All we have to do is add
$this->actingAs()
: - And we are back to passing. If you are using
$this->call()
instead of$this->visit()
then you can use$this->be()
instead:
How it works…
Pretty nice how Laravel brings all of...