是否可以在 Simpletest 中设置模拟对象的属性

发布于 2024-08-30 16:47:57 字数 620 浏览 3 评论 0原文

我通常在我的对象上使用 getter 和 setter 方法,并且我可以通过使用以下代码操作它们来在 SimpleTest 中将它们作为模拟对象进行测试:

Mock::generate('MyObj');
$MockMyObj->setReturnValue('getPropName', 'value')

但是,我最近开始使用魔术拦截器 (__set() __get()) 和访问属性像这样:

$MyObj->propName = 'blah';

但是我很难使模拟对象具有使用该技术访问的特定属性。

那么是否有一些特殊的方法来设置 MockObjects 的属性。

我尝试过这样做:

 $MockMyObj->propName = 'test Value';

但这似乎不起作用。不确定是否是我的测试主题、模拟、魔术拦截器或 SimpleTest 导致该属性无法访问。

因此,总而言之:

我可以在模拟对象上模拟方法,但在模拟模拟对象的类属性时遇到困难。是否可以在 Simpletest 中设置模拟对象的属性?

欢迎任何建议。

I normally use getter and setter methods on my objects and I am fine with testing them as mock objects in SimpleTest by manipulating them with code like:

Mock::generate('MyObj');
$MockMyObj->setReturnValue('getPropName', 'value')

However, I have recently started to use magic interceptors (__set() __get()) and access properties like so:

$MyObj->propName = 'blah';

But I am having difficulty making a mock object have a particular property accessed by using that technique.

So is there some special way of setting properties on MockObjects.

I have tried doing:

 $MockMyObj->propName = 'test Value';

but this does not seem to work. Not sure if it is my test Subject, Mock, magic Interceptors, or SimpleTest that is causing the property to be unaccessable.

So, in summary:

I can mock-up methods on my mock objects but i am having trouble mocking-up class properties of mock objects. Is it possible to set properties on a Mock Object in Simpletest?

Any advice welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

合约呢 2024-09-06 16:47:57

回答我自己的问题...

是的,可以设置使用魔术拦截器的对象的模拟属性 - 只需设置拦截器方法的返回值,就像使用任何其他方法一样。

SimpleTest 示例模拟模拟对象上截获的属性:

对于此对象,

class MyObj 
   {

   public function __set($name, $value)
    {
    $props[$name] = $value;
    }

   public function __get($name)
    {
    return $props[$name] = $value;
    }

   }

客户端(测试的聚合器类)可以像这样访问属性,

$MyObj->propName = 'blah';
echo $MyObj->propName; //prints blah

并且可以像这样模拟

Mock::generate('MyObj');
$MockMyObj = new MockMyObj();
$MockMyObj->setReturnValue('__get', 'test property value', array('propName'));

//...later on...
echo $MockMyObj->propName; //prints "test property value"


以下是一些有关模拟的更多文档:
http://www.simpletest.org/en/mock_objects_documentation.html

PPS

我做了实际上之前尝试过,但我的实验因代码中的拼写错误而变得模糊。

In answer to my own question...

Yes it is possible to set the properties of mocks of objects that use magic interceptors - just set the return value of the interceptor method like you would with any other method.

SimpleTest Example Mocking Intercepted Properties on Mocked Objects:

for this object

class MyObj 
   {

   public function __set($name, $value)
    {
    $props[$name] = $value;
    }

   public function __get($name)
    {
    return $props[$name] = $value;
    }

   }

a client (tested aggregator class) can access the properties like so

$MyObj->propName = 'blah';
echo $MyObj->propName; //prints blah

and it can be mocked like so

Mock::generate('MyObj');
$MockMyObj = new MockMyObj();
$MockMyObj->setReturnValue('__get', 'test property value', array('propName'));

//...later on...
echo $MockMyObj->propName; //prints "test property value"

P.S.
Here are some more docs about mocks:
http://www.simpletest.org/en/mock_objects_documentation.html

P.P.S

I did actually try it before, but my experimentation was obscured by a typo in my code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文