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

Is it possible to print errors inside validation exception in unit test?

Is it possible to print the message out from the error bag when testing API's using unit test? Yes it already shows the exception but it does not specify exactly what and in which validation the error is. To make it clear, please have a look at…
Yura
  • 1,937
  • 2
  • 19
  • 47
1
vote
0 answers

Notification fake assertion not working on password reset test?

I was trying to make tests for my auth routes. For password reset route I am trying to make in which I am faking the notification module of laravel and asserting as per the docs. This is my test file public function…
1
vote
1 answer

Laravel mock multiple Guzzle endpoints

I'm using Laravel 6 and trying to test an endpoint. The endpoint is making 2 requests to external API's (from mollie). Currently I mock it like this: Abstract BaseMollieEndpointTest
Jenssen
  • 1,801
  • 4
  • 38
  • 72
1
vote
1 answer

Laravel unit testing if an event is triggered when an eloquent model event is triggered

I am developing a Laravel application and writing tests as I go along the way. Now, I am having a problem writing a test that is asserting if an event is triggered when an eloquent model event is triggered. This is what I am doing. This is my…
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
1
vote
1 answer

Laravel Telescope does not record requests coming from TestCase

For debugging purpose, I would like to have Telescope record any requests coming from Testing suite. Telescope does not at the moment and I have no ideas why. I enabled telescope in phpunit.xml This is…
Michael Nguyen
  • 1,691
  • 2
  • 18
  • 33
1
vote
1 answer

Strange assertJSON behaviour

I want to test, whether an AJAX response contains the array I'm expecting. So far so good, not really a great deal. This is how my array should look like: array ( 'data' => array ( 0 => array ( 'key1' => 'value1', 'key2'…
TimSch
  • 1,189
  • 1
  • 12
  • 27
1
vote
2 answers

How to Set Up a Test for the Destroy Method Laravel 5.6

I'm going through the testing on my app, and I've gotten to the point where I need to test the destroy method of my controller. The test as I have it currently is: public function test_user_can_delete_draft() { $user =…
EJ Willis
  • 89
  • 1
  • 11
1
vote
1 answer

laravel testing fails when testing passport oauth2 with virtual database

I'm using virtual test database for my testing. my API is working with postman. but creating problem when writing test. when I execute the test it shows a long list of error containing the following message below- "message": "Client error: POST…
hardcoderrsl
  • 371
  • 3
  • 5
1
vote
1 answer

Laravel testing. Reset controller DI while test

I have a Laravel controller with DI in __construct(Model1 $m1, Model2 $m2, $SomeService $s) { $this->m1 = $m1; $this->m2 = $m2; $this->s = $s; } I have an API test with call or endpoint based on this controller. I'm doing 8 same API url calls…
Denys Siebov
  • 198
  • 4
  • 16
1
vote
1 answer

Mock Curl response for testing in laravel

I am trying to unit test a function(updateMeeting) in laravel which uses a curl request inside it. I tried many google links but nothing worked. can you guys give me suggestions on this matter. following are relevant methods. $this->meeting is a…
Deemantha
  • 433
  • 9
  • 30
1
vote
0 answers

laravel unit test seeJson is fail with child level structure

I used phpunit to test the Laravel API. The API has return result like { success: true, data: { 'attr1': 'value1', 'attr2': 'value2' } } when I used: seeJson([ 'success' => true ]) it's pass, but when I used…
Ki Ko
  • 324
  • 1
  • 16
1
vote
1 answer

Laravel faking file in the factory not working

I am developing a Web application using Laravel. I am unit testing my application that involves file operation. Have a look at my scenario below. I have a download file action in the controller like this public function downloadPersonalDetails(Order…
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
1
vote
1 answer

How can I edit divs with 'contenteditable' property with Laravel Dusk?

I have a wysiwyg editor in my project, that I need to test and it has a contenteditable property. I cant type directly, as it is a simple tag:
naneri
  • 3,771
  • 2
  • 29
  • 53
1
vote
1 answer

How to include a CSRF Token in get route in Laravel tests?

To prevent CSRF attacks, I'm adding a state parameter with my current CSRF token to an external link. When the external page redirects the user back to my site, it will include the state. With a simple condition I can check it in my…
Philipp Mochine
  • 4,351
  • 10
  • 36
  • 68
1
vote
1 answer

Laravel 5.6. Mockery. Mock method on property

Currently I have something like that (simplified version) app(ShopifySDK::class)->Order->get($params) How can I mock this class and check get() method input? This is not working. $this->app->bind(ShopifySDK::class, function () { return…
D.R.
  • 2,540
  • 3
  • 24
  • 49