首页 > 解决方案 > Symfony - 添加了测试失败的方法

问题描述

我的 Symfony 测试环境有问题。

测试失败。

我在控制器中添加了另一种方法:

$this->aService->methodA($param1, $param2);
$this->bService->methodB($param1, $param2);

使用方法 A,我的测试工作正常。然后我在下面添加了方法 B,测试正在中断。

断言两个字符串相等时失败。--- 预期 +++ 实际 @@ @@ -'成功' +'错误'

这是我的测试方法:

$this->forMethodA
        ->expects($this->once())
        ->method('methodA')
        ->will($this->returnCallback(function($contact, $tags) {
        }));

    $this->client->request(
        Request::METHOD_POST,
        '/api/method-a',
        [],
        [],
        [
            'CONTENT_TYPE' => 'application/json',
            'ACCEPT' => 'application/json',
        ],
        json_encode($this->user)
    );
    $response = $this->client->getResponse();
    $responseBody = json_decode($response->getContent(), true);

    $this->assertArrayHasKey('status', $responseBody);
    $this->assertEquals('success', $responseBody['status']);
    $this->assertArrayHasKey('data', $responseBody);
    $this->assertEquals($this->forMethodA['id'], $responseBody['data']['userId']);
    $this->assertEquals(200, $response->getStatusCode());

它正在工作,但是在特定 API 调用中添加 methodB() 时,测试失败。

MethodB 实际上是为类保存新数据的方法。

  public function methodB($param1, $param2) {

    $new = new EntityObject();
    $new->setParam1($param1);
    $new->setParam2($param2);

    $this->entityManager->persist($new);
    $this->entityManager->flush();

    return $new;
  }

我的问题是,我是否需要在这个测试方法中为我添加的新方法添加一些东西来模拟该行为?

标签: phpunit-testingsymfonymockingphpunit

解决方案


推荐阅读