我正在使用Symfony 3.3和PHPUnit 5.7,而我正在尝试模拟测试api控制器的服务。
控制器:
class ApiTestManager extends BaseApiController{
public function getAction(): View
{
$response = $this->get('app.business.test_api')->getResponse();
return $this->view($response);
}}
测试类:
class ApiTestManagerTest extends WebTestCase {
public function testApiCall()
{
$client = static::createClient();
$service = $this->getMockBuilder(ApiTestManager::class)
->disableOriginalConstructor()
->setMethods(['getResponse'])
->getMock()
->expects($this->any())
->method('getResponse')
->will($this->returnValue(new Response()));
$client->getContainer()->set('app.business.test_api', $service);
$client->request('GET', 'de/api/v1/getResponse');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}}
我花了好几个小时试图找到错误,但每次执行此测试时都会出现以下错误:
Error: Call to undefined method PHPUnit_Framework_MockObject_Builder_InvocationMocker::getResponse()
有谁能告诉我我的代码有什么问题?谢谢:))
答案 0 :(得分:1)
也许?这是我唯一看到的......
$service = $this->getMockBuilder(ApiTestManager::class)
->disableOriginalConstructor()
->setMethods(['getResponse'])
->getMock();
$service->expects($this->any())
->method('getResponse')
->will($this->returnValue(new Response()));