phpunit测试assertNotRegExp()未按预期工作


phpunit test assertNotRegExp() not working as expected?

测试失败,断言"{"error":{"message":"Book not found"}}"与PCRE模式"/Book not found/"不匹配。

为什么此模式与内容字符串不匹配

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

模式/Book not found/与内容字符串{"error":{"message":"Book not found"}}匹配。这是正确的。

请注意,您使用的是assertNotRegExp(),也就是说,您实际上是在说"确保模式与字符串不匹配"。因此,断言在模式不匹配时成功,在模式匹配时失败。

似乎你真的想在测试中使用assertRegExp()

相关文章: