php - 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;
}
我的问题是,我是否需要在这个测试方法中为我添加的新方法添加一些东西来模拟该行为?
解决方案
推荐阅读
- ruby-on-rails - Rails 5:上传后附件自动提交
- node.js - 发生错误。错误是:错误:模块版本不匹配。预期 48,得到 64
- javascript - JavaScript 循环范围和代码执行(端口扫描器)
- c# - 更改第二个文本框时保留除以零消息
- django - 即使保存到数据库后密码也不会散列
- apache-spark - kafka max.poll.records 在火花流中不起作用
- python - Pandas Dataframe .loc 布尔附加索引到第一列
- jquery - 如何在jQuery中按名称删除带有子哈希的哈希
- json - jmeter - 如何在正文数据中格式化 json(从正则表达式中提取)
- python-3.x - 为什么我的 python 脚本不会在端口 36296 上启动服务器?