Summer Sale! All accounts are 50% off this week.

duffn's avatar
Level 6

assertSeeText with newlines

I have a simple feature test that looks something like this.

/** @test */
public function user_likes_page_shows_your_when_user_authenticated()
{
    $user = User::factory()->create();

    $this
        ->actingAs($user)
        ->get(route('likes', ['user' => $user]))
        ->assertStatus(200)
        ->assertSeeText('Your liked items');
}

I'm using a Blade component to generate a portion of this view and so my Your liked items text outputs like this in the content.

 \n
                              \n
                                  Your\n
   liked items\n
                              \n
                          \n

I've tried numerous variations of assertSeeText and assertSee in order to check that my output contains Your liked items but have yet to be able to find the right combination.

How can I assert that my view simply contains Your liked items regardless of it having newlines?

0 likes
3 replies
Corsi's avatar

Hello,

Did you ever find a solution to this? I am facing the exact same issue, and cannot find out how to make it work properly...

ju5t's avatar

I haven't tested this but I would probably have it fetch the $request->getContent(), trim() it, and assert that in a separate assertion.

If you want a repeatable solution you can add a macro to TestResponse. Copy assertSeeText and within tap() you could replace:

PHPUnit::assertStringContainsString((string) $value, $content);

with:

PHPUnit::assertStringContainsString((string) $value, trim($content));

I did not test test but I think it would work.

bluedreamer's avatar

Had the same thing - this is a crappy workaround I thought of

->assertSeeInOrder(explode(' ', '60 variations of 50 birds on 6 stations with: 1 x three pair, 3 x four pair, 2 x five pair'));

Please or to participate in this conversation.