用于模拟新类对象调用的方法的单元测试
我正在为现有代码编写单元测试,如下所示这里
class someClass {
public function __construct() { ... }
public function someFoo($var) {
...
$var = "something";
...
$model = new someClass();
model->someOtherFoo($var);
}
public someOtherFoo($var){
// some code which has to be mocked
}
}
我应该如何模拟对函数“someOtherFoo
”的调用,以便它不会执行“some code”在
someOtherFoo
里面?
class someClassTest {
public function someFoo() {
$fixture = $this->getMock('someClass ', array('someOtherFoo'));
$var = "something";
....
// How to mock the call to someOtherFoo() here
}
}
是否可以模拟构造函数,以便它返回我自己构造的函数或变量?
谢谢
I am writing unit test for an existing code which is like this
class someClass {
public function __construct() { ... }
public function someFoo($var) {
...
$var = "something";
...
$model = new someClass();
model->someOtherFoo($var);
}
public someOtherFoo($var){
// some code which has to be mocked
}
}
Here how should I be able to mock the call to function "someOtherFoo
" such that it doesn't execute "some code
" inside someOtherFoo
?
class someClassTest {
public function someFoo() {
$fixture = $this->getMock('someClass ', array('someOtherFoo'));
$var = "something";
....
// How to mock the call to someOtherFoo() here
}
}
Is it possible to mock out the constructor so that it returns my own constructed function or variable?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
无论您在测试方法中的何处有
new XXX(...)
,您都注定失败。将实例化提取到同一类的新方法 --createSomeClass(...)
。这允许您创建被测类的部分模拟,该模拟从新方法返回存根或模拟值。在测试中,在调用
someFoo()
的实例中模拟createSomeClass()
,并在调用someFoo()
的实例中模拟someOtherFoo()
从第一个模拟调用返回。请记住,由于该实例正在创建同一类的另一个实例,因此通常会涉及两个实例。如果更清楚的话,您可以在这里使用相同的模拟实例。
Wherever you have
new XXX(...)
in a method under test, you are doomed. Extract the instantiation to a new method--createSomeClass(...)
--of the same class. This allows you to create a partial mock of the class under test that returns a stubbed or mock value from the new method.In the test, mock
createSomeClass()
in the instance on which you callsomeFoo()
, and mocksomeOtherFoo()
in the instance that you return from the first mocked call.Keep in mind that because the instance is creating another instance of the same class, two instances will normally be involved. You could use the same mock instance here if it makes it clearer.
您可以在模拟类名称前添加
overload:
前缀,查看 模拟硬依赖。
您的示例如下:
我添加了
@runTestsInSeparateProcesses
注释,因为通常模拟的类也会在其他测试中使用。如果没有注释,自动加载器将因类已存在
错误而崩溃。如果这是您的测试套件中唯一使用模拟类的地方,那么您应该删除该注释。
You can prefix your mock class name with
overload:
Check out the docs on Mocking Hard Dependencies.
Your example would be something like:
I added the
@runTestsInSeparateProcesses
annotation because usually the mocked class will be used in other tests too. Without the annotation, then the autoloader will crash because of theclass already exists
error.If this is the one and only place that mocked class is used in your test suite, then you should remove the annotation.
我在这里找到了尝试对类 __constructor 进行白盒测试的方法,以确保它调用自身的类方法,并将一些数据传递到 __constructor。
如果其他人出于同样的原因在这里,我想我会分享我最终使用的方法(没有在这个问题中使用工厂风格的 createSomeClass() 方法)。
现在进行 PHPUnit 测试:
I found my way here attempting to white-box test a class __constructor to make sure it calls a class method on itself, with some data passed in to the __constructor.
In case anyone else is here for the same reason, I thought I would share the method I ended up using (without the factory-style createSomeClass() method used in this question).
Now the PHPUnit test: