First test with the Pest testing framework with Laravel

This video is part of the complete course on Laravel, as I had already told you, I completely translated the test from the test category to what would be from php unit for pest, so I'm going to show you what changes I made, indicating that it's practically all the same, that is, there is no substantial change to the section of an assertion method that did not exist or does not exist in the Pest API and therefore I had to use one that was equivalent or that worked or we could implement it in a similar way. So here I'm going to show you how to do it completely.

Let's remember a little that here in pest we are using or defining this function to generate our authentication token:

tests/Pest.php

uses(TestCase::class, RefreshDatabase::class)->in('Feature');

function generateTokenAuth()
{
    User::factory()->create();
    return User::first()->createToken('myapptoken')->plainTextToken;
}

From there we can use the method anywhere without any problem, so here we have the test method that allows just as we saw, testing the one to obtain the page categories without pagination, which is what we have here, they are all and you can see that there is no change, the same with Ok and making JSON and for the rest here is what the Factory is to generate the Data:

test('test all', function () {

    Category::factory(10);
    $categories = Category::get()->toArray();

    $this->get(
        '/api/category/all',
        [
            'Authorization' => 'Bearer ' . generateTokenAuth()
        ]
    )->assertOk()->assertJson($categories);
});

Then, make the request, just like we do with PHPUnit:

$this->get(
        '/api/category/all',
        [
            'Authorization' => 'Bearer ' . generateTokenAuth()
        ]

And here we pass the token here the only thing that changes is that we no longer use the header token to define the header but we pass it directly as a parameter and this is something we already did before the Test by ID token is exactly the same as I was telling you:

'Bearer ' . generateTokenAuth()

Remember that if you have any questions you can leave them in the comments block but I am not going to explain the same thing again because we have already done it several times and again it is the same here the same generate the test data the tests the assertion methods are exactly the same and for the dep more or less the same this one is to test create Well we also implemented this one similar before or sorry we implemented this one before, so I skip it and here is where we have the change this method:

$this->assertStringContainsString("The title field is required.", $response->getContent());

It does not exist, instead we use the following method that evaluates based on regular expressions:

$this->assertMatchesRegularExpression("/The title field is required./", $response->getContent());

So that's what we're telling you to look for the answer here. Note that they are the same functions as contain, so we don't have any problems here and well, it works the same way again here. I left you the previous code in the same way only for this test so that you can compare or use it to do your own tests here, the same thing again, we change the method a little, the same as doing contains here. Nothing changes. Doing content, doing status. Well, this one. Remember that I use either the Ok one or the 404 one, it's going to be status:

assertOk()

It depends on which one exists to compare the status code there are several and the put one is very similar to the creation one and we continue going down and here we finish everything as you can see we have nothing new here the only thing I did was a small refactoring to the class is no longer a class the file in which I removed the response and placed all the codes here. Finally here we can see that everything works so you can see that I'm not cheating I put something wrong and now it's going to fail then you can see that obviously our tests that we have defined here are working so well nothing now with this I think that in the same way in a demonstrative way or well not demonstrative but now in a practical way it seems interesting to me to do this same thing but I think it's going to be a video to do the step by step of doing the translation in case someone has some doubts out there but again you should do all this yourself without any problem and if you have any doubts you can go check the source code and there see in case that again you have someone doing the migration well see how I did it a possible implementation or if not directly watch the video so nothing without more to say let's go to the next class.

- Andrés Cruz

En español

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.

!Courses from!

10$

On Udemy

There are 0d 18:51!


Udemy

!Courses from!

4$

In Academy

View courses

!Books from!

1$

See the books
¡Become an affiliate on Gumroad!