bencarter78@hotmail.com's avatar

PHPUnit test passing then failing if dummy data is entered

Hi all

So I have something I don't understand and was hoping one of you laravel-ers could help me.

I have written a test to see if I can trash a model. This is what I have...

class SurveysControllerTest extends TestCase
{
    use DatabaseTransactions;

    function user()
    {
        return factory(\App\UserManager\Users\User::class)->create();
    }

    function survey()
    {
        return factory(\App\SurveyHound\Survey::class)->create();
    }

    /** @test */
    function it_trashes_the_survey()
    {
        $survey = $this->survey();

        $this->actingAs($this->user())
             ->visit('/surveyhound')
             ->see($survey->title)
             ->press('Delete')
             ->seePageIs('/surveyhound')
             ->see('You have successfully trashed the survey')
             ->dontSee($survey->title);
    }

Now if I have an empty table in my database then the code passes, however if I go onto my web app and enter a new survey, then run my test again it fails.

Can anyone explain why this happens?

Be kind I'm new to testing!

0 likes
3 replies
kfirba's avatar

@bencarter78 fails with what error message? Maybe your code is trying to grab the first survey thus the tests are asserting against the manual inserted survey instead of the test-created survey? More details of the failing test is needed in order to help you

bencarter78@hotmail.com's avatar

Hi @kfirba The error is...

Failed asserting that 

...
<table class="table table-striped table-bordered">
    <tr>
        <th>Title</th>
        <th>Description</th>
        <th>Frequency</th>
        <th class="text-center">Actions</th>
    </tr>
    <tr>
        <td>Vel qui ab neque ipsam cum.</td>
        <td>Iusto veniam aut est architecto eum fuga cum.</td>
        <td>12</td>
        <td class="text-center">
            <div class="actions">
                <a class="btn btn-circle btn-primary btn-xs" href="http://vandango.app/surveyhound/200/edit"><i class="fa fa-pencil"></i></a>
                <a class="btn btn-circle btn-danger btn-xs" role="button" data-target="#modal200" data-toggle="modal"><i class="fa fa-trash"></i></a>
            </div>
        </td>
    </tr>
</table>

...
does not match PCRE pattern "/(Vel qui ab neque ipsam cum\.|Vel qui ab neque ipsam cum\.)/i

It seems like it's trashing the first record it finds rather than the one it created.

bencarter78@hotmail.com's avatar

Ah I've just realised my error, I didn't specify which 'Delete' button to press so it's pressing the first one it can find which is of courses the already inserted one.

Thanks for helping me realise my error!

Please or to participate in this conversation.