Ryderpro
Ryderpro

Reputation: 1315

phpunit test assertNotRegExp() not working as expected?

Test failed asserting that '{"error":{"message":"Book not found"}}' does not match PCRE pattern "/Book not found/".

Why is this pattern not matching the content string?

<?php

namespace Tests\App\Http\Controllers;

use TestCase;

class BooksControllerTest extends TestCase
{
    /** @test **/
    public function show_route_should_not_match_an_invalid_route()
    {
        $this->get('/books/this-is-invalid');

        $this->assertNotRegExp(
            '/Book not found/',
            $this->response->getContent(),
            'BooksController@show route matching when it should not.'
        );
    }
}

Upvotes: 0

Views: 357

Answers (1)

Andrey Tserkus
Andrey Tserkus

Reputation: 3634

The pattern /Book not found/ does match the content string {"error":{"message":"Book not found"}}. That works correctly.

Note that you're using assertNotRegExp() - i.e. you're literally saying - "Make sure that the pattern does not match the string". So the assertion succeeds when the pattern does not match, and fails when the pattern matches.

Seems like you actually wanted to use assertRegExp() for your test.

Upvotes: 4

Related Questions