Mockery测试构造函数调用,新关键字(Mockery testing constructor call, new keyword)

编程入门 行业动态 更新时间:2024-10-25 12:15:47
Mockery测试构造函数调用,新关键字(Mockery testing constructor call, new keyword)

我有一个类名作为字符串属性。 一个方法( 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.

更多推荐

本文发布于:2023-07-30 22:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340314.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   关键字   测试   Mockery   keyword

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!