我有以下控制器,它的路由包含一个通配符
    /**
     * @Route("api/1.0/test/{$a}", name="test", methods={"GET"})
     * @return JsonResponse
     */
    public function testSimple(int $a)
    {
        return $this->json(['success' => ($a == 1) ? true : false]);
    }我正在尝试测试如果通配符$a = 1则返回true,但是我似乎找不到正确的语法来测试路由:
A-作为完整路径,通配符设置为要传递的值:
    /**
     * @test
     */
    public function checkRouteWildcardReturnsExepctedResult()
    {
        $client = $this->createClient();
        $request = $client->request('GET','api/1.0/test/1');
        $response = $client->getResponse()->getContent();
        $this->assertEquals(json_encode(["success" => true]), $response);
    }
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'{"success":true}'
+'{"success":false,"errors":["No route found for GET \/api\/1.0\/test\/1"],"data":[]}'B-准确指定路由,找到正确的路由,但未设置$a
    /**
     * @test
     */
    public function checkRouteWildcardReturnsExepctedResult()
    {
        $client = $this->createClient();
        $request = $client->request('GET','api/1.0/test/{$a}',['a' => 1]);
        $response = $client->getResponse()->getContent();
        $this->assertEquals(json_encode(["success" => true]), $response);
    }
--- Expected
+++ Actual
@@ @@
-'{"success":true}'
+'{"success":false,"errors":["Could not resolve argument $a of [__CLASS__]::testSimple(), maybe you forgot to register the controller as a service or missed tagging it with the controller.service_arguments?"],"data":[]}'任何链接到一些文档的建议,我似乎找不到非常欣赏的-干杯。
发布于 2020-10-08 18:12:54
找到问题后,选项1是正确的解决方案,并且路由通配符不应包含$,应为@Route("api/1.0/test/{a}", name="test", methods={"GET"})
所以它确实能像我最初预期的那样工作
https://stackoverflow.com/questions/64260166
复制相似问题