warpig's avatar
Level 12

Something wrong with my first test file?

Hello, all my tests seem to be failing no matter what and im wondering if perhaps something is wrong with the code below.

Following this tutorial: https://laracasts.com/series/build-a-voting-app/episodes/10 I did everything as André himself said, we had to first and foremost create an idea via php artisan make:test IdeaShowTest command, afterwards uncomment this portion: <server name="DB_CONNECTION" value="sqlite"/> from phpunit.xml and then write the following code below:

<?php

namespace Tests\Feature;

use Tests\TestCase;
use App\Models\Idea;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ShowIdeasTest extends TestCase
{
    use RefreshDatabase;

    public function list_of_ideas_shows_on_main_page()
    {
        $ideaOne = Idea::factory()->create([
            'title' => 'My First Idea',
            'description' => 'Description of my first idea',
        ]);

        $ideaTwo = Idea::factory()->create([
            'title' => 'My Second Idea',
            'description' => 'Description of my second idea',
        ]);

        $response = $this->get(route('idea.index'));

        $response->assertSuccessfull();
        $response->assertSee($ideaOne->title);
        $response->assertSee($ideaOne->description);
        $response->assertSee($ideaTwo->title);
        $response->assertSee($ideaTwo->description);
    }
}

But as it turns out it's not working and I don't know why. I'll share with you a portion from the messages that I got, in total there should be 15 errors and it's obvious to me, seems like it wants to know about my database but Im at a loss here since this is my first time checking out the tests functionality within Laravel, so was there a database prerequisite other than uncommenting that line on phpunit.xml? Should I had added a new env var ? I read through the docs but couldn't find anything worth mentioning.. Please let me know, thanks!

3) Tests\Feature\Auth\AuthenticationTest::test_users_can_not_authenticate_with_invalid_password
Illuminate\Database\QueryException: Database (ratemyclip) does not exist. (SQL: PRAGMA foreign_keys = ON;)

Caused by
InvalidArgumentException: Database (ratemyclip) does not exist.

4) Tests\Feature\Auth\EmailVerificationTest::test_email_verification_screen_can_be_rendered
Illuminate\Database\QueryException: Database (ratemyclip) does not exist. (SQL: PRAGMA foreign_keys = ON;)

5) Tests\Feature\Auth\EmailVerificationTest::test_email_can_be_verified
Illuminate\Database\QueryException: Database (ratemyclip) does not exist. (SQL: PRAGMA foreign_keys = ON;)

Caused by
InvalidArgumentException: Database (ratemyclip) does not exist.

There was 1 warning:

1) Warning
No tests found in class "Tests\Feature\ShowIdeasTest".

ERRORS!
Tests: 16, Assertions: 0, Errors: 15, Warnings: 1.
0 likes
11 replies
Tray2's avatar

Have you installed SQLite 3 on your computer? Have you uncommented both these lines in your phpunit.xml file?

        <!-- <server name="DB_CONNECTION" value="sqlite"/> -->
        <!-- <server name="DB_DATABASE" value=":memory:"/> -->
warpig's avatar
Level 12

@Tray2 SQLite is not installed I think, I only uncommented the first line.

Tray2's avatar

@warpig Then you need to install it and uncomment the second line as well.

1 like
warpig's avatar
Level 12

What does this mean?

PHPUnit 9.5.10 by Sebastian Bergmann and contributors.

...............W                                                  16 / 16 (100%)

Time: 00:01.094, Memory: 36.50 MB

There was 1 warning:

1) Warning
No tests found in class "Tests\Feature\ShowIdeasTest".

WARNINGS!
Tests: 16, Assertions: 25, Warnings: 1.
frankielee's avatar

@warpig Check your others succeed test functions

should be test +functionName()

test_list_of_ideas_shows_on_main_page()

1 like
warpig's avatar
Level 12

@frankielee I think this made it work, however I still had an extra "l" in $response->assertSuccessfull(); after that it was all good.

frankielee's avatar

@warpig

however I still had an extra "l" in $response->assertSuccessfull();

Sorry, I don't really understand. Would you wind to elaborate a bit?

Tray2's avatar

Or add the test in your doc block like this

    /**
    * @test
    */
    public function it_throws_a_404_if_the_post_is_not_published_yet(): void
    {
        Post::factory()->create([
            'title' => 'My first post',
            'published_at' => Carbon::now()->addWeek()
        ]);
        $this->get('/blog/my-first-post')
            ->assertStatus(404);
    }
1 like

Please or to participate in this conversation.