我为自己创建了一个自定义 PHPUnit 约束并将其连接到一个不错的 assert... 函数。
我将它放入我的基本 TestCase 中,它是 assertLastError 函数:
/**
* abstract, base test-case class.
*/
abstract class TestCase extends \PHPUnit_Framework_TestCase
{
/**
* assert lint of a php file
*/
protected function assertLint($filename, $message = '') {
self::assertThat($filename, new ConstraintLint, $message);
}
/**
* assert the last error
*/
protected function assertLastError($error, $file, $message = '') {
self::assertThat($file, new ConstraintLastError($error), $message);
}
/**
* assert XML DTD validity
*/
protected function assertXmlStringValidatesDtdUri($xml, $dtd, $message = '') {
self::assertThat($dtd, new ConstraintXmlStringValidatesDtdUri($xml), $message);
}
...
到目前为止,我已经调试了约束,我看到 evaluate 方法被调用并返回 FALSE,但是测试运行器没有向我报告失败。
约束条件:
/**
* ConstraintLastError
*
* For asserting the last error message in a file.
*
* To test trigger_error().
*
* Example:
*
* $this->assertLastError('Error Message', 'basenameOfFile.php');
*
*/
class ConstraintLastError extends \PHPUnit_Framework_Constraint {
private $file;
private $error;
public function __construct($error) {
$this->error = $error;
}
/**
* Evaluates the constraint for parameter $file. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
* @param string $file Value or object to evaluate.
* @return bool
*/
public function evaluate($file)
{
$this->file = $file;
$error = $this->error;
$lastError = error_get_last();
if (NULL === $lastError)
return false;
$last_message = $lastError['message'];
$last_file = $lastError['file'];
$result = ($error == $last_message && basename($file) == basename($last_file));
var_dump($result, $error, $last_message, $file, $last_file);
return $result;
}
/**
* @param mixed $other
* @param string $description
* @param boolean $not
*/
protected function customFailureDescription($other, $description, $not)
{
return sprintf('Failed asserting that the last error %s', basename($other), $not ? '' : 'no ', implode("\n - ", $this->lines));
}
/**
* Returns a string representation of the constraint.
*
* @return string
*/
public function toString()
{
return sprintf('was %s in file %s.', $this->error, $this->file);
}
}
我不知道为什么它停止工作,测试用例刚刚干净地完成,我可以在输出中看到错误消息,包括。堆栈跟踪(xdebug 已打开)和 var_dump 告诉我结果是 FALSE。这是测试:
public function testGetType()
{
...
$fragment->setParsed(array());
\PHPUnit_Framework_Error_Warning::$enabled = FALSE;
$actual = $fragment->getType();
\PHPUnit_Framework_Error_Warning::$enabled = TRUE;
$this->assertLastError('Invalid State Error FAIL', 'Fragment.php');
}
这是我刚刚写的一个新测试,我在其他地方也有同样的断言,但它们不再有效。
PHPUnit 3.6.7
最佳答案
我现在可以解决这个问题。这与我升级了 PHPUnit 有关,API 略有变化。我再次将 PHPUnit_Framework_Constraint 作为我自己约束的模式,阅读其中的评论会有所帮助。
evaluate 函数现在有所不同,我现在将评估逻辑移到了一个私有(private)函数中,并从 evaluate 切换到了 matches。它与返回值一起使用。 evaluate 默认情况下不再使用返回值,但会抛出异常。要从中充分受益,您还可以实例化一些比较器对象,但这超出了我的理解范围,您可以在 asserEquals 约束中找到更多相关信息。
class ConstraintLastError extends \PHPUnit_Framework_Constraint {
...
/**
* Evaluates the constraint for parameter $file. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
* This method can be overridden to implement the evaluation algorithm.
*
* @param mixed $other Value or object to evaluate.
* @return bool
*/
public function matches($file)
{
return $this->compareAgainstLast($file, $this->error);
}
/**
*
* @param string $file
* @param string $error
* @return bool
*/
private function compareAgainstLast($file, $error)
{
if (!$last = error_get_last())
{
$last = array('message' => '(none)', 'file' => '');
}
$this->lastError = $last['message'];
$this->lastFile = $last['file'];
return $error === $this->lastError && basename($file) === basename($this->lastFile);
}
/**
* @param string $file
*/
protected function failureDescription($file)
{
return sprintf('the last error is "%s" in %s, was "%s" in %s'
, $this->error, basename($file)
, $this->lastError, basename($this->lastFile)
);
}
...
它现在就像一个魅力:
1) FragmentTest::testGetType
Failed asserting that the last error is "Suboptimal State Error" in Fragment.php, was "Invalid State Error" in Fragment.php.
其他人也有类似的问题,但切换到 fail 的解决方案不同,您也可以调用它,因为它是在基类中实现的 Phake Fixes issues #43 and #44 .
关于php - 自定义 PHPUnit 约束停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9456884/
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin