【发布时间】:2019-03-03 01:20:28
【问题描述】:
【问题讨论】:
-
例如通过发布未通过
Employees表的验证/应用程序规则的数据。 -
或者您想知道在这种情况下如何编写 Flash 消息生成的断言?
标签: unit-testing cakephp-3.0 phpunit
【问题讨论】:
Employees 表的验证/应用程序规则的数据。
标签: unit-testing cakephp-3.0 phpunit
您最好在响应正文上执行断言。毕竟,Flash 消息只不过是 div 中的文本。一个成功的 Flash 输出示例:
<div class="message success" onclick="this.classList.add('collapse')">Entity saved</div>
任何成功或错误消息的可能断言可能如下所示:
$this->assertResponseContains('<div class="message success"');
$this->assertResponseContains('<div class="message error"');
或者您可以复制整个预期的 Flash 输出。
【讨论】:
从 CakePHP 3.7 开始,您可以使用 assertFlashMessage 和其他一些助手,还有一些 examples in the manual
对于您的情况,您的测试代码可能如下所示:
class EmployeesControllerTest extends TestCase
{
use IntegrationTestTrait;
public function testEmployeeAddErrorMessage()
{
$bad_data = ['bad' => 'data'];
$this->post('/employees/add', $bad_data);
$this->assertFlashMessage('The employee could not be saved. Please, try again.');
}
}
【讨论】: