【问题标题】:assertEqual not works when testing equals of command output测试等于命令输出时,assertEqual 不起作用
【发布时间】:2015-01-31 10:03:20
【问题描述】:

这是我在命令中输出的内容:

$output->writeln("\033[37;42m Translations from " . $input->getArgument('bundle') . " imported successfully! \033[0m");

这是我在命令行中使用 trim func 删除空格的内容:

trim($commandTester->getDisplay())
..string(69) " Translations from AcmeDemoBundle imported successfully! "

但是当我运行这个时:

$this->assertEquals(" Translations from AcmeDemoBundle imported successfully! ", trim($commandTester->getDisplay()));

我明白了:

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-' Translations from AcmeDemoBundle imported successfully! '
+Binary String: ... 

我做错了什么?

【问题讨论】:

  • trim 命令没有做任何事情,因为它默认只删除空格字符。如果要检查字符串的内容,请使用 unpack 函数。

标签: php unit-testing symfony phpunit command-line-interface


【解决方案1】:

这是预期的行为,因为输出中的字符串是:

"\033[37;42m Translations from AcmeDemoBundle imported successfully! \033[0m"

在字符串的开头和结尾都有特殊字符,但它们不是 trim 函数删除的特殊字符,所以,你真正的比较是在上面的字符串和:

 " Translations from AcmeDemoBundle imported successfully! "

哪些是不同的字符串,虽然你在输出中看不到特殊字符。

您可以在指定 trim 函数的第二个属性的字符串中添加要删除的字符。您可以在此处查看规范:

http://php.net/manual/es/function.trim.php

【讨论】:

  • 这应该可以工作:$this->assertEquals(" Translations from AcmeDemoBundle imported successfully! ", trim($str, "\033[37;42m\033[0m"));
猜你喜欢
  • 2022-08-03
  • 1970-01-01
  • 2012-09-08
  • 2022-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多