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

Failed asserting that a row in the table student.sections matches the attributes

Hello im new to PHPUnit with minimum knowledge in laravel. Im trying to test this method that mass create student section public function setStudentsSection(Request $request) { $enrollments = Enrollment::whereIn('student_id',…
Kael
  • 161
  • 2
  • 13
0
votes
2 answers

laravel dusk:install file_get_contents(): SSL operation failed

Trying to install dusk on Laravel like this: php7.4 artisan dusk:install I get this error: Dusk scaffolding installed successfully. Downloading ChromeDriver binaries... ErrorException file_get_contents(): SSL operation failed with code 1.…
iateadonut
  • 1,951
  • 21
  • 32
0
votes
1 answer

laravel 8 TDD $response->assertSessionHasNoErrors() is not working

The Illuminate\Testing\TestResponse::assertSessionHasNoErrors() isn't working as I would expect it to. I have something like this in my code: $response = $this->post('account/project/create', $array); $response->assertStatus(200); //…
iateadonut
  • 1,951
  • 21
  • 32
0
votes
1 answer

How to assert array value but not strict?

How can we assert some of the array's property values which contain the expected object values? My code below is working okay, but it checks all array property values. I want to ask if there's a way we can check only some of it. $dataToBeTested =…
0
votes
1 answer

How do i get logged in user in another test case in laravel phpUnit testing?

When I check dd(\Auth()::check()); in testLoginTrue method in LoginTest case then it's returning true but when I use dd(\Auth()::check()); in testClientCreateFormDisplayed method in ClientTest then it's returning false. So how can I get a logged…
Hardik Patel
  • 11
  • 1
  • 2
0
votes
1 answer

Laravel 5.7 testing with RefreshDatabase trait throws ErrorException: Trying to access array offset on value of type int

so as I stated in the title, I am working on Laravel 5.7 project and am making first tests in this application (big system). We did not make any tests in here yet, so this problem is the first time here. For every test, this is how the controller…
Lukas Grofcik
  • 457
  • 1
  • 3
  • 15
0
votes
1 answer

Testing insert methods by using factory make of Laravel with a PostgreSQL reserved id

Our Laravel app uses PostgreSQL and before we insert a register in a table we reserve the next valid id by calling PostgreSQL nextval function. I want to test the insert method of UserRepository. As our database expects to receive an id I would need…
assensi
  • 352
  • 1
  • 6
  • 17
0
votes
1 answer

Login as multiple users during same test - Laravel and PestPHP

I'm trying to run a single test where multiple users are performing actions. There is either a bug in the actingAs() function, or I am completely missing something trivial. So here's the scenario. I have a social media site. I want to login as one…
eResourcesInc
  • 948
  • 1
  • 9
  • 17
0
votes
2 answers

How can I make fake records in a way that some of them have specific values in laravel?

I need to make some fake records in laravel testing in a way that some of them have specific values. for example I need to create 20 fake records of countries name and want to name the two records "USA" and "UK" and other values is not…
rahman j89
  • 59
  • 8
0
votes
1 answer

i made a testing class in laravel and using factory in it to make a user but it is giving error that "fname" doesn't have a default value

public function test_case1() { $user = factory(User::class)->create([ 'fname' => "Testing", 'lname' => 'bot', 'email' => "testing_bot@apimio.com", 'password' => bcrypt("testing_bot"), ]); …
0
votes
1 answer

Laravel feature test using middleware

I'm trying to make a feature test for the start page of my Laravel application. Route: Route::domain('{subdominio}.'.env("APP_DOMAIN"))->middleware('mapNumserie')->group(function () { Route::get('/',…
jssDev
  • 923
  • 5
  • 18
0
votes
1 answer

How to test laravel login event

Trying to write a test for my LoginListener. As the name states, it listens for the login event and then logs a record of this in the activity_log table. When I try run the test it throws the below error: LoginListener::handle(): Argument #1…
J Foley
  • 1,038
  • 1
  • 17
  • 30
0
votes
1 answer

Laravel tests fails under SQLite if uses MySQL function in query

In my Laravel project I use some MySQL based SQL functions in my queries, like this: Post::whereRaw('publish_at < NOW()'); SELECT * FROM posts WHERE publish_at < NOW(); Or this: Post::whereRaw('TIMESTAMP(publish_date, publish_time) <…
netdjw
  • 5,419
  • 21
  • 88
  • 162
0
votes
2 answers

Laravel8 tdd: session is missing expected key errors

I just want to test an easy input field but I get this error! /** @test */ public function email_must_be_a_valid_email() { $response = $this->post('/api/contacts', array_merge($this->data(), ['email' => 'NOT AN EMAIL'])); …
Mohsen
  • 260
  • 3
  • 14
0
votes
2 answers

PHPUnit test fails with InvalidArgumentException: Unknown formatter with Laravel 8 factory

In my Laravel 8 project, I have this action class:
netdjw
  • 5,419
  • 21
  • 88
  • 162