RonB1985's avatar

Test sometimes passes, sometimes fails

I am purely focussing on testing since I haven't done that up until now. And for this I am just following the "Let's build a forum" series. I have done everything that Jeff did as well, but I am getting unexpected results. For starters, in his second lesson:

https://laracasts.com/series/lets-build-a-forum-with-laravel/episodes/2

The most simple test is returning a 404 instead of a 200.

$response = $this->get('/categories');
$response->assertStatus(200);

When I change it to the following:

        $category = factory('App\Category')->create();

        $response = $this->get('/categories');

        $response->assertSee($category->name);

It works every once in a while. But it fails every 2-3 times before it passes once. I am pretty sure it has to do something with my setup. I am using windows, and I am using xampp. To run the actual tests, I had to create a bat file in my project folder with the following:

@ECHO OFF
SET BIN_TARGET=%~dp0/vendor/phpunit/phpunit/phpunit
php "%BIN_TARGET%" %*

This works, but like I said, I am getting unexpected results. When it fails, it also returns the 404 page. This is what is being displayed in my cmd:

    <body>\n
        <div class="flex-center position-ref full-height">\n
            <div class="content">\n
                <div class="title">\n
                    Sorry, the page you are looking for could not be found.                </div>\n
            </div>\n
        </div>\n
    </body>\n
</html>\n
' contains "aut".

C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestResponse.php:251
C:\xampp\htdocs\project\tests\Feature\CategoriesTest.php:19

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.

Just browsing to the /categories route works fine. I have set up the route correctly, also the controller, including the view.

0 likes
4 replies
shez1983's avatar
shez1983
Best Answer
Level 23

what i do in these cases (when test sometimes fail) is dump out the response body.. and then re run the tests until they fail to see why they are..

I dont see a reason why this should be failing unless you are doing something funky in the factory..

3 likes
RonB1985's avatar

Yeah, it's strange. But I opted to do things differently, and I am using Homestead now, which is working a lot better so, I'll stick to that :)

Thanks for your answer.

mehdian92's avatar

Sometimes you must to run:

php artisan cache:clear

before running your test

Please or to participate in this conversation.