我有一个类名作为字符串属性。 一个方法( fetchObject )创建此类的新实例。 我想用PHPUnit和Mockery测试这个方法。
那是代码
class Maker { private $_className = 'Article'; public function fetchObject( $array ) { return new $this->_className( $array ); } }我试过这样的事
$data = array( 'title' => 'Amazing title' ); $article = m::mock( 'Article', array( $data ) ); $maker= new Maker; $result = $maker->fetchObject( array() );测试工作,但我不确定它是否检查构造函数调用,我不知道如何检查param是否通过。
I have a class name as a string property. One method (fetchObject) creates new instance of this class. I would like to test this method with PHPUnit and Mockery.
That's the code
class Maker { private $_className = 'Article'; public function fetchObject( $array ) { return new $this->_className( $array ); } }I tried something like this
$data = array( 'title' => 'Amazing title' ); $article = m::mock( 'Article', array( $data ) ); $maker= new Maker; $result = $maker->fetchObject( array() );Test works but I'm not sure if it checks for constructor call and I don't know how to check if param was passed.
最满意答案
通常你会模拟Maker()类,并确保它从mock,你正在寻找的对象返回。
我为测试这样的事情做的另一个选择是简单地测试Maker并使用给定的类期望调用fetchObject()例程,然后使用InstanceOf检查返回的对象是否是所需的类。
Normally you would mock the Maker() class, and ensure it returned from the mock, the object you are looking for.
The other option I have done to test things like this, is to simply test Maker and call the fetchObject() routine with a given class expectation, and then test with an InstanceOf check that the returned object is of the desired class.
更多推荐
发布评论