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
2
votes
0 answers

Assert laravel model observer's event fired in PHPUnit

I'm going to write some tests for my laravel 7 controller methods (endpoints) to be sure that they fire model observer event (for example saved event on Product model) as you now model observer events do not fire while using mass update and it's…
SoheilYou
  • 907
  • 5
  • 23
  • 43
2
votes
1 answer

Laravel 6/7 testing: A facade root has not been set

I am trying to write some tests for my my Laravel 7 app. But i keep facing A facade root has not been set. error.
Amin Shojaei
  • 5,451
  • 2
  • 38
  • 46
2
votes
1 answer

Laravel Notification asserting the data passed down to mail markdown view

I am working on a Laravel project. I am writing integration/ feature tests for my application. I am now writing a test where I need to assert the data passed to the email notification and the data passed to its view. I found this link to do it,…
2
votes
1 answer

How can I assert an instance of a ResourceCollection in Laravel?

I am working with a feature test and it's returning data correctly; all things are coming back correctly; and I'm at the final portion of my test. I am struggling to assert that I'm getting back a…
Damon
  • 4,151
  • 13
  • 52
  • 108
2
votes
0 answers

Laravel Model Factory - Large JSON Stubs

I am pulling order information from an external API and saving the entire JSON response into a documents table. I obviously do not want to call this API when testing so I have setup a factory, but I am struggling with how to pull in these JSON…
Adam Lambert
  • 1,311
  • 3
  • 24
  • 45
2
votes
3 answers

Laravel middleware sharing variable view does not work when writing HTTP test

We have the following middleware. We know the middleware works since testing the application in browser is working. Sadly when writing HTTP test case, blade is saying that the variable defined by the middleware is not there.
RageZ
  • 26,800
  • 12
  • 67
  • 76
2
votes
0 answers

In Laravel 5.8, how do you define a model factory in a package?

I can't seem to find any good and up to date resources on how to accomplish this. I basically want to do this from within my package: $factory->define(MyPackage\About\Models\About::class, function (Faker $faker) { return [ "title" =>…
Kevin Pimentel
  • 2,056
  • 3
  • 22
  • 50
2
votes
1 answer

Phpunit : hadling the validation exception in laravel

I have turned throw Exception in handler.php so that I can catch exceptions and see the errors but what happened is when I try to do the validation checks it throws me an exception which is correct but in my test case instead of catching the…
rakesh shrestha
  • 1,335
  • 19
  • 37
2
votes
1 answer

Laravel 5.7 Testing with Passport in seeder

I have a UsersTableSeeder which generates a Passport personal token: $user = factory(User::class)->create([ 'email' => 'someone@example.com', ]); Artisan::call('passport:client', [ '--personal' => 'default', '--no-interaction' =>…
2
votes
1 answer

Laravel testing - UnauthorizedException: User does not have the right roles

So I'm working with laravel tests and I'm making tests for accessing users page where I can see all the users and edit them. In the UsersController.php i have made that only admin can access the user's page. The roles and permissions I've used from…
2
votes
1 answer

phpunit command gives permission denied error on laravel project

When i try to execute vendor/bin/phpunit i get error -bash: vendor/bin/phpunit: Permission denied on laravel 5.2 project. I tried deleting entire vendor folder and re installing with composer install but the error occurs same. is there any…
Deemantha
  • 433
  • 9
  • 30
2
votes
2 answers

Laravel PHP Unit Testing

I am just testing my laravel app with phpunit When i run vendor/bin/phpunit i am getting error like below Error: Call to undefined method ExampleTest::assertStatus() Below is the code i was trying to execute $response = $this->json('POST',…
06011991
  • 797
  • 2
  • 13
  • 39
2
votes
0 answers

Laravel testing without firing cache events

I am using the array cache driver while testing, however I want to disable the Cache Events. I can do this with $this->withoutEvents(); I'd rather just stop the one event,…
Edward Louth
  • 1,010
  • 2
  • 9
  • 19
2
votes
2 answers

Laravel PHPUnit: how to test url prefix

I'm learning to build some PHPUnit tests. My app redirects to default language when no prefix is used, for instance: www.myapp.com has 301 redirect to www.myapp.com/en, while www.myapp.com/en has a 200 OK. Trying to make tests to my Controller, i…
Diogo Mendonça
  • 903
  • 2
  • 12
  • 29
1
vote
2 answers

Counting the items in multiple sub-arrays in Laravel tests using assertJsonCount ()

For a Laravel Test of my Product API I am trying to check if the right number of prices are listed for all products. The API returns Json and the prices are items in the products.*.prices arrays. I am new to Laravel Test. How do I loop through all…
St. Jan
  • 284
  • 3
  • 17
1 2
3
11 12