我分叉了Command /Command,并添加了一个命令模块检查,它会引发异常。在测试时,这个异常会导致测试失败,但我正在运行一个测试,以强制异常和失败,以确保它捕获它。
下面是对src/coL.3/Command/GenerateSuite.php的编辑
if ($this->containsInvalidCharacters ($suite)) {
throw new \Exception("Suite name '{$suite}' contains invalid characters. ([A-Za-z0-9_]).");
}在更新test/cli/GenerateSuiteCept.php时,当我运行命令$I->executeCommand('generate:suite invalid-suite');时,它会失败,因为首先我会在src/coL.3/ command /GenerateSuite.php中抛出异常。
下面是对test/cli/GenerateSuiteCept.php的添加:
$I->executeCommand('generate:suite invalid-dash-suite');当我运行它时,它说它失败了。但是,我希望它能够传递,因为我故意添加破折号来验证它捕获了无效字符。做这件事的最好方法是什么?我对这些测试中的尝试/捕捉阻塞非常谨慎。我不确定这是不是最好的方法。
发布于 2014-11-20 05:41:40
在查看了其他looking测试之后,我发现这是一个比抛出异常更好的方法:
if ($this->containsInvalidCharacters ($suite)) {
$output->writeln("<error>Suite name '{$suite}' contains invalid characters. ([A-Za-z0-9_]).</error>");
return;
}因此,我修改了测试,以便:
$I->expect ('suite is not created due to dashes');
$I->executeCommand('generate:suite invalid-dash-suite');
$I->seeInShellOutput('contains invalid characters');https://stackoverflow.com/questions/27019705
复制相似问题