如何在 phpunit 的 setUpBeforeClass 方法中创建模拟对象?
我想在所有测试中都有一个模拟对象,所以我尝试在 setUpBeforeClass() 方法中创建它,但这个方法是静态的,所以 getMockBuilder 必须像这样静态调用:
public static function setUpBeforeClass() {
self::mocked_object = self::getMockBuilder('MockedClass')
->disableOriginalConstructor()
->getMock();
}
问题是 getMockBuilder 不能静态调用:
Argument 1 passed to PHPUnit_Framework_MockObject_MockBuilder::__construct() must be an instance of PHPUnit_Framework_TestCase, null given
是否有机会在 setUpBeforeClass 方法中构建模拟对象,或者我必须每次在测试之前构建它(在 public function setUp() 方法中)?
I want to have an mocked object in all my tests, so I try to create it in the setUpBeforeClass() method, but this method is static so getMockBuilder has to be called statically like this:
public static function setUpBeforeClass() {
self::mocked_object = self::getMockBuilder('MockedClass')
->disableOriginalConstructor()
->getMock();
}
The problem is that getMockBuilder cannot be called statically :
Argument 1 passed to PHPUnit_Framework_MockObject_MockBuilder::__construct() must be an instance of PHPUnit_Framework_TestCase, null given
Is there any chance to have the mocked object be built in the setUpBeforeClass method or do I have to build it every time before a test (in the public function setUp() method) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每个模拟对象都与创建它的测试用例实例相关联。由于
setUpBeforeClass()
是静态方法,因此它没有实例,无法创建模拟对象。相反,请在
setUp()
或辅助方法中创建模拟,并将它们分配给实例变量或返回它们。Each mock object is tied to the test case instance that created it. Since
setUpBeforeClass()
is a static method, it does not have an instance and cannot create mock objects.Instead, create your mocks in
setUp()
or helper methods and either assign them to instance variables or return them.