Questions tagged [laravel-testing]

Use this tag for question about Laravel testing

Laravel is built with unit testing in mind. In fact, support for testing with PHPUnit is included out of the box, and a phpunit.xml file is already setup for your application. An example test file is provided in the tests directory.

Defining & Running Tests

To create a test case, simply create a new test file in the tests/Unit or tests/Feature directory. The test class should extend TestCase. You may then define test methods as you normally would when using PHPUnit.

An Example Test Class

class FooTest extends TestCase {

    public function testSomethingIsTrue()
    {
        $this->assertTrue(true);
    }

}

You may run all of the tests for your application by executing the php artisan test (or phpunit) command from your terminal.

Note: If you define your own setUp method, be sure to call parent::setUp.

Reference

177 questions
8
votes
1 answer

How to inspect the actual Laravel Command output while writing tests?

I was writing a very basic test for a Laravel Artisan Console Command, like this: $this->artisan("my-command", ["--some-option" => "some-value"]) ->expectsOutput("the expected output"); The test didn't pass. I got really troubled because "the…
8
votes
3 answers

Laravel not generating code coverage report

I am using Laravel 8 and to test my app, I am running php artisan test --coverage-html reports The tests are running successfully. The problem is that there is no coverage reports generated. I have gone through answers here on SO and solution is…
Owen Kelvin
  • 14,054
  • 10
  • 41
  • 74
8
votes
1 answer

Illuminate\Validation\ValidationException : The given data was invalid. Called when trying to get the json from a response while testing

I have the following test: public function testStoreMemberValidation() { $response = $this->withExceptionHandling()->post('/api/members', [ "name" => "Eve", "age" => "invalid" ], ['X-Requested-With' => 'XMLHttpRequest']); …
thodic
  • 2,229
  • 1
  • 19
  • 35
7
votes
1 answer

Laravel Testing: assertJsonFragment fails if multiple levels need to be checked

This is the response: [ { "data":{ "locales":{ "translate":[ { "created_at":"2018-05-28 12:49:53", "deleted_at":null, "id":1, "key":"nl_NL", …
GiamPy
  • 3,543
  • 3
  • 30
  • 51
7
votes
1 answer

Test Queue functionality?

According to the Laravel Documentation, I can use Queue::fake(); prevent jobs from being queued. What is not clear how to test (PHPUnit) a few methods in the Job Class while it is not being queued. For example: class ActionJob extends Job { …
I'll-Be-Back
  • 10,530
  • 37
  • 110
  • 213
5
votes
0 answers

Fake only specific event don't work in Laravel test

I'm using the package spatie/laravel-sluggable in my project. I need to assert that an event was dispatched. The problem is when I fake the event (Event::fake([AdvertPublished::class])) in the test, in theory only my Advertpublished event should be…
Jairo
  • 131
  • 1
  • 11
5
votes
5 answers

Laravel 5.6. How to test JSON/JSONb columns

$this->assertDatabaseHas() not working with JSON/JSONb columns. So how can I tests these types of columns in Laravel? Currently, I have a store action. How can I perform an assertion, that a specific column with pre-defined values was…
D.R.
  • 2,540
  • 3
  • 24
  • 49
5
votes
3 answers

Unable to find PHPUnit\Framework\Constraint\Constraint in Laravel BrowserKit package after upgrade to 5.4

I'm upgrading an old Laravel personal project from 5.2 to 5.4. The upgrade to 5.3 seems to have gone OK, but now I'm moving to 5.4 I've run into an issue. The project used the old testing layer so I've installed the BrowserKit testing package to…
Matthew Daly
  • 9,212
  • 2
  • 42
  • 83
5
votes
4 answers

How to get Response variables in a Laravel PHPUnit test?

I am testing a controller method and I am accessing a route in a test. Then I would like to make sure that the correct model was returned in the view and was loaded with all of the correct relationships. I know that I can do…
user3494047
  • 1,643
  • 4
  • 31
  • 61
5
votes
1 answer

Laravel unit testing show complete error

I am trying to create a test for my laravel 5.2 project which will test a register page in a rest resource controller. When I test it manually with the exact input of my test everything works but when I test it with phpunit I get redirected to an…
Stefan
  • 323
  • 2
  • 15
4
votes
1 answer

Laravel phpunit tests Cannot find TestCase object on call stack

where running all test php artisan test everything work as expected and all tests runs now , when i run signle test php artisan test --filter test_get_profile i get this wired error An error occurred inside PHPUnit. Message: Cannot find TestCase…
d3ridi
  • 117
  • 1
  • 7
4
votes
0 answers

Laravel RefreshDatabase not working when running a test

I created a feature test api. CategoryApiTest.php namespace Tests\Feature; use App\Models\Category; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Http\JsonResponse; use Tests\TestCase; class CategoryApiTest extends TestCase { …
Huan Ho
  • 145
  • 2
  • 9
4
votes
1 answer

How to test Laravel / Sanctum endpoint with CSRF tokens

I have a headless laravel instance connected to a SPA on a subdomain. As my first test, I want to see a 401 when trying retrieve user data from the api without being logged in. My api path is defined as the…
Shane
  • 4,921
  • 5
  • 37
  • 53
4
votes
1 answer

Laravel test not passing

So I'm learning doing tests for my application and one of the tests it doesn't want to pass, and here it is the logic: Basically, when a user request the home page, I expect that the database list count would be 0, and this passed, then I expect…
Cod3rMax
  • 75
  • 5
4
votes
3 answers

How to test/assert if an event is broadcasted in Laravel

I am developing a Laravel application. I am using Laravel Broadcast in my application. What I am trying to do now is that I am trying to test if an event is broadcasted in Laravel. I am broadcasting an event like this: broadcast(new…
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
1
2 3
11 12