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
1
vote
0 answers

Laravel testing: get() method persists query string

I'm running Laravel 5.6 and came across a weird issue while testing end points with query string. Here's my test: /** * @test */ public function returns_records_filtered_by_search() { …
Sebastian Sulinski
  • 5,815
  • 7
  • 39
  • 61
1
vote
1 answer

Laravel 5.6 - how to create a model factory with a vertical table?

I am working on application that is made up of Leads, each Lead -> hasMany -> Fields. My application will accept an infinite amount of whitelisted fields. Some Leads will have a lot of Fields, others will have maybe around 5. Due to this, I've opted…
Kingsley
  • 977
  • 2
  • 11
  • 27
1
vote
2 answers

How to mock time() function while testing in Laravel?

I'm doing a maths contest project using Laravel. All of the controller methods in the project uses a lot of time() functions. Questions are returned to the user depending on whether the current time is in between the contest live time. While…
Benedict
  • 360
  • 1
  • 6
  • 15
1
vote
1 answer

Laravel 5 PHPUnit Testing - Wrapping All Tests Within a Controller Into a Single Transaction

I have the following test file in my Laravel application: use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseTransactions; class…
Lloyd Banks
  • 35,740
  • 58
  • 156
  • 248
1
vote
1 answer

setUp method not working laravel 5.4

My integration test works fine without a setup method. That is the factories work and data is populated into the table.
Yeasir Arafat Majumder
  • 1,222
  • 2
  • 15
  • 34
1
vote
1 answer

Acces to config variable when testing a package in laravel

I'm writing a test in Laravel and I want to unittest this piece of code: if (file_exists(\Config::get('maintenance.dir.api'))) { throw new ServiceUnavailableException('We are down for maintenance'); } I'm using…
Fran Roa Prieto
  • 385
  • 1
  • 3
  • 12
1
vote
1 answer

Mockery: Method does not exist on mock object

So I have an object which I am mocking and it has methods that are used for sending emails. I have gone from a static class over to a concrete class because I was having issues testing the static class with Mockery. However I am now finding that…
Joseph Crawford
  • 1,470
  • 1
  • 15
  • 29
1
vote
1 answer

How to Call command line from TestCase in Laravel 5

I am developing an application in Laravel 5, I have a test file which extends from TestCase.php, I need to call the phpcs command in my file class MyTest extends TestCase { public function testFunction() { //here I need to call…
Siavosh
  • 2,314
  • 4
  • 26
  • 50
1
vote
1 answer

Laravel 4.2 : testing with assertRedirectedToRoute fails

I was wondering if anyone else came across this problem. I'm going through Jeffrey Way's book on testing in Laravel and I'm on the chapter which explains how to test controllers. When I follow the examples from the book - I get the message: Failed…
Sebastian Sulinski
  • 5,815
  • 7
  • 39
  • 61
0
votes
1 answer

How to make custom exception in http unit test?

On laravel site in controller I catch custom KeyIsNotProvidedException Exception: method(); ... } catch (KeyIsNotProvidedException $e) { return response()->json([ …
Petro Gromovo
  • 1,755
  • 5
  • 33
  • 91
0
votes
1 answer

How to show a reference to the failing test when calling $this->fail()

I'm extending Laravel's base TestCase class to include my own assertion. Something like this:
miken32
  • 42,008
  • 16
  • 111
  • 154
0
votes
1 answer

Laravel Package - Experiencing different data of model between in the test file and in the controller

I'm currently working on a laravel package that has route configuration below Route::apiResource('companies', Controllers\CompanyController::class); Route::apiResource('companies.addresses',…
Fery W
  • 1,402
  • 1
  • 15
  • 28
0
votes
0 answers

Laravel factory does not load global scope

I cannot figure out why global scope isn't loading with the factory during the tests: class User extends Authenticatable { //... /** * The "booted" method of the model. */ protected static function booted(): void { …
Sebastian Sulinski
  • 5,815
  • 7
  • 39
  • 61
0
votes
1 answer

Problem for run test on a Laravel package

I use a transformed Spatie/laravel-package-skeleton with my own ModuleServiceProvider and Module class for my system. I didn't find suffisant ressource on the web for making my module system so maybe there is a problem of implementation. Normaly ( I…
0
votes
1 answer

How can I make Laravel Dusk run seeders consistently?

I have been working on an issue with Laravel Dusk for a few hours and I cannot seem to find a solution. To start, I have a few tests written for the home page of my application. These tests all succeed individually (commenting out all but one) but…