我有函数测试,它返回属于单独类
的静态函数staticABC的repsonsefunction test()
{
return testA::staticABC();
}
现在我想通过模拟staticABC()函数来编写用于函数测试的PHPUnit案例。任何技术人员都可以对此有所了解吗?
答案 0 :(得分:1)
我不认为有一种模拟函数的方法,但你可以做的是使用某种test double:
class SUT{
$staticCreator = array('testA::staticABC'); //Initialized to a default
//for production, would be better if injected somehow before using
function setStaticCreator($staticCreator){
$this->staticCreator=$staticCreator;
}
function test(){
return call_user_func($this->staticCreator);
}
}
然后以这种方式运行测试:
class Test extends ...{
function mockStaticABC(){
return "mock_string";
}
test_testfunction(){
$sut = new SUT();
$staticCreator = array($this,'mockStaticABC');
$sut->setStaticCreator($staticCreator);
$mock_return = $sut->test();
$this->assertEquals("mock_string",$mock_return);
}
}