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
1 answer

How with breeze to make feature test for forgot-password?

In laravel 9, breeze 1.11 app I want to make feature test for forgot-password functionality and in routes I found : GET|HEAD In laravel 9, breeze 1.11 app I want to make feature test for forgot-password functionality and in routes I found…
mstdmstd
  • 2,195
  • 17
  • 63
  • 140
1
vote
1 answer

How to checks raised exceptions in phpunit tests?

In laravel 9.26.1 app I make tests with phpunit ^9.5.10 and I want to make checks on raised exceptions on login with invalid credentials In app/Http/Requests/Auth/LoginRequest.php I see : public function authenticate() { …
mstdmstd
  • 2,195
  • 17
  • 63
  • 140
1
vote
0 answers

laravel auth testing without Authenticatable User

I'm newbie on testing so I want to create a auth testing but in project I don't use laravel Models but I'm using own methods for auth because of started project too long ago. TestCase class has actingAs method but i don't use it so i can not test…
1
vote
2 answers

How to test array contains only objects with PHPUnit?

I'm looking for solution to test an array of objects with PHPUnit in my Laravel project. This is my haystack array: [ [ "id" => 10, "name" => "Ten" ], [ "id" => 5, "name" => "Five" ] ] And this is the…
netdjw
  • 5,419
  • 21
  • 88
  • 162
1
vote
1 answer

get data Factory Relationships for Laravel Testing

according laravel-database-testing I have a factory relationship for testing data, my code like this public function test_users_can_authenticate_using_the_login_screen() { $this->seed(RoleSeeder::class); $data =…
baralogi
  • 27
  • 5
1
vote
0 answers

unitary test laravel and php

it´s my first time that I´m doing the unitary test in PHP and laravel and I have this problem: Tests\Vocces\Company\Routes\CreateNewCompanyRouteTest > post create new company route Expected status code 201 but received 404. Failed asserting that…
scorpions78
  • 553
  • 4
  • 17
1
vote
1 answer

Laravel testing url to resource in public directory

I'm using Laravel Dusk to test all links on a page, these links are PDF files that are that are copied from resources/docs to public/docs with laravel mix. The problem is that trying to access the URL of the docs using get(uri) method gives a 404,…
hereForLearing
  • 1,209
  • 1
  • 15
  • 33
1
vote
1 answer

InvalidArgumentException on Laravel controller testing

In my Laravel project I have this test: /** @test */ public function tagControllerStore() { $this->post(action([TagController::class, 'store']), [ 'name' => 'My tag', 'type' => 'My type', …
Ferenc Bablena
  • 445
  • 1
  • 4
  • 12
1
vote
1 answer

General error: 1 no such table: App\Models\ModelName while testing laravel

My test: class FloorStackTest extends TestCase { use RefreshDatabase, WithFaker, DatabaseMigrations; protected $endPoint = '/endpoint'; public function test_unit_type_note_added_successfully() { $this->signIn(); …
Aayush Dahal
  • 856
  • 1
  • 17
  • 51
1
vote
0 answers

Laravel: How to check every element attribute value of an array of JSON data in unit testing

I am trying to test if the filter response has the right JSON data (every item should have the filtered group_code). I tried the following code but it doesn't work. public function test_filter_items_by_group_code_work_properly() { $filterCode…
Omar Fayad
  • 1,733
  • 3
  • 11
  • 27
1
vote
1 answer

Laravel Test Case Error - Call to a member function where() on null

I have defined relationship in the ModelClass public function allocations(): MorphMany { return $this->morphMany( Allocation::class, 'allocatable', 'ref_class', 'ref_id' )->where('is_active', 1); //…
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
1
vote
1 answer

Laravel Feature/Unit Testing

Since I am still rather a beginner in Laravel, I just stumbled upon an interesting issue with testing and need a second opinion. I wrote models, controllers, views, factories, seeders and then (feature) tests. Tests do cover a lot (won't say all),…
Kristjan O.
  • 814
  • 1
  • 9
  • 33
1
vote
2 answers

Mocking an api call for testing in Laravel

I have a controller that calls an api outside like this: public function getUserData(Request $request) { // api connection is established using guzzle and into $client variable $userData = $client->get($request->user_id); // ... process…
shaNnex
  • 1,043
  • 2
  • 19
  • 34
1
vote
1 answer

Pass parameter to GET request in laravel tests

In Laravel tests i want to send a get request with some parameters like this: $response=$this->get( route('orders.payment.pay',['order'=>$order->id]), ['pay_type','payment_gateway'] ); but when i run it, i…
1
vote
1 answer

Laravel factory create without calling afterCreating callback

While writing tests I'm creating a model using factory $recipe = factory(Recipe::class)->create() but the RecipeFactory has afterCreating callback that runs and adds relations every time I create a recipe. Is there a way to skip this callback? I…
Salam
  • 1,126
  • 14
  • 20