是否可以在 Simpletest 中设置模拟对象的属性
我通常在我的对象上使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回答我自己的问题...
是的,可以设置使用魔术拦截器的对象的模拟属性 - 只需设置拦截器方法的返回值,就像使用任何其他方法一样。
SimpleTest 示例模拟模拟对象上截获的属性:
对于此对象,
客户端(测试的聚合器类)可以像这样访问属性,
并且可以像这样模拟
它
以下是一些有关模拟的更多文档:
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
a client (tested aggregator class) can access the properties like so
and it can be mocked like so
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.