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

Encountered Method Illuminate\Auth\RequestGuard::attempt does not exist when running php artisan test

I have this code public function it_can_change_password() { DB::beginTransaction(); $payloadForUser = [ "name" => "Name", "first_name" => $this->faker->firstName, "last_name" =>…
Fil
  • 8,225
  • 14
  • 59
  • 85
0
votes
1 answer

Laravel PHPUnit testsuite class not found when used from vendor folder

I have added a Laravel package as local repository to my composer.json: ... "repositories": [{ "type": "path", "url": "../external-tests" }], "require": { ... "shaedrich/external-tests": "dev-develop", …
shaedrich
  • 5,457
  • 3
  • 26
  • 42
0
votes
0 answers

I got “There is no active transaction” error after I added RefreshDatabase into test file

Making in laravel 9.48.0 with mysql database http tests after I added RefreshDatabase into test file use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\Artisan; use…
mstdmstd
  • 2,195
  • 17
  • 63
  • 140
0
votes
1 answer

How do I repeat the same test on a feature test in laravel

This is my feature test class CreateImageTest extends TestCase { private static function headers(){ .... } /** * @test * */ public function no_api_key_404() { ...... } /** * @test …
Melly
  • 675
  • 8
  • 24
0
votes
0 answers

How to use Laravel AssertJson for testing a structured response

I have a signup API that returns a JSON response in this kind of format { "meta": { //response metadata }, "data": { //user object } } I want to test this response with AssertableJson but I'm only concerned about the…
Melly
  • 675
  • 8
  • 24
0
votes
1 answer

Why custom Exception was not catch by expectException method?

Making tests on laravel 9 site I try to catch custom Exception and looking at this Undefind withoutExceptionHandling() example I do in my tests
Petro Gromovo
  • 1,755
  • 5
  • 33
  • 91
0
votes
0 answers

How to test Auth::once() in Laravel?

I'm using Auth:once() to authenticate users for a single request. if (!Auth::once($this->only('email', 'password'))) { RateLimiter::hit($this->throttleKey()); throw ValidationException::withMessages([ 'email' => __('auth.failed'), …
Bookie
  • 1
  • 1
0
votes
1 answer

How to test Laravel/Lumen API when retrieving the logged in user's ID in controller constructor?

I'm trying to test CRUD operations for my Lumen controllers. The constructor for each controller looks similar to this: private $loggedInUserId; public function __construct(Request $request) { $this->loggedInUserId =…
0
votes
0 answers

How to use MongoDB in Laravel Testing

I have laravel project, which I'm using two database: relational PostgresSQL and MongoDB. I have to write feature tests to database but. I don't know how to create connection with mongo from laravel testing. My phpunit.xml fragment file
0
votes
0 answers

Php unit cover files of composer package

i run tests in my laravel project but the test files are inside a composer package, here is my section of phpunit.xml ./vendor/mypackage/package/tests/Unit The…
Luca Becchetti
  • 1,210
  • 1
  • 12
  • 28
0
votes
0 answers

Cannot use object of type Illuminate\Support\Facades\Config as array error while running test commad with coverage report

I have one project with Laravel 9 and I am performing testing with coverage report on it, The test cases successfully passed but I'm getting the below error, and if I remove --coverage-html tmp/coverage from command it's working fine. It means this…
0
votes
1 answer

How to assert If the signature is valid in the url - Laravel, TDD

I have a rating & review form which is used by the customers to submit their reviews. This form can be accessed using a url even if they are not signed-in to the platform. I use signed routes to prevent anyone from submitting the form. The url is…
Murlidhar Fichadia
  • 2,589
  • 6
  • 43
  • 93
0
votes
0 answers

PHPUnit in Laravel removes old database records when executing new test method

I created a Laravel test class with PHPUnit which has two methods as below, class MainLocationTest extends TestCase { use RefreshDatabase, TestUserTrait; public static function getMainLocationCount(): int { return…
0
votes
0 answers

How to mock laravel model relathipship?

I have a model that has a relationship with a View, that is complicate to popolate for make the feature test, but in the same time this is called from some component that are inside the controller called. The following code is an example:
0
votes
0 answers

How to test required fields in post route against the rules?

I have a test to test, if a client can checkin with all required fields: public function test_client_can_checkin() { $client = Client::factory()->create(); $response = $this->post('/', $client->toArray()); …
JanBoehmer
  • 395
  • 3
  • 14