0

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.'
        );
    }
}

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hmmm. Working through "Writing API's with Lumen." I found I misread a section about updating the route. Fixed the duplicate route with $app->get('/books/{id:[\d]+}', 'BooksController@show'); and now test works. Route does not match that expression it will not be routed to the controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.