PhpUnit ::如何测试受保护变量的__construct?
(并不总是我们应该添加公共方法getVal() - soo而不添加返回受保护变量值的方法)
例:
class Example{
protected $_val=null;
function __construct($val){
$this->_val=md5 ($val);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
在返回void的函数中也存在测试问题
EDIT2:
我们需要测试__construct的原因示例:
class Example{
protected $_val=null;
//user write _constract instead __construct
function _constract($val){
$this->_val=md5 ($val);
}
function getLen($value){
return strlen($value);
}
}
class ExampleTest extends PHPUnit_Framework_TestCase{
test_getLen(){
$ob=new Example();//call to __construct and not to _constract
$this->assertEquals( $ob->getLen('1234'), 4);
}
}
Run Code Online (Sandbox Code Playgroud)
测试运行正常,但没有创建示例类"构造函数"!
谢谢
单元测试的主要目标是测试接口默认情况下,您应该只测试公共方法及其行为.如果没关系,那么你的课程可以用于外部使用.但有时您需要测试受保护/私有成员 - 然后您可以使用Reflection和setAccessible()方法
归档时间: |
|
查看次数: |
1256 次 |
最近记录: |