如何在 phpunit 的 setUpBeforeClass 方法中创建模拟对象?

发布于 2024-12-10 15:54:10 字数 580 浏览 1 评论 0原文

我想在所有测试中都有一个模拟对象,所以我尝试在 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 技术交流群。

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

发布评论

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

评论(1

坠似风落 2024-12-17 15:54:10

每个模拟对象都与创建它的测试用例实例相关联。由于 setUpBeforeClass() 是静态方法,因此它没有实例,无法创建模拟对象。

相反,请在 setUp() 或辅助方法中创建模拟,并将它们分配给实例变量或返回它们。

class MyServiceTest extends PHPUnit_Framework_TestCase
{
    function setUp() {
        $this->connection = $this->getMock('MyDatabaseConnection', array('connect', ...));
        $this->connection
                ->expects($this->once())
                ->method('connect');
    }

    function testLogin() {
        $this->connection
                ->expects($this->once())
                ->method('login')
                ->with('bob', 'p4ssw0rd');
        $service = new MyService($this->connection);
        self::assertTrue($service->login('bob', 'p4ssw0rd'));
    }
}

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.

class MyServiceTest extends PHPUnit_Framework_TestCase
{
    function setUp() {
        $this->connection = $this->getMock('MyDatabaseConnection', array('connect', ...));
        $this->connection
                ->expects($this->once())
                ->method('connect');
    }

    function testLogin() {
        $this->connection
                ->expects($this->once())
                ->method('login')
                ->with('bob', 'p4ssw0rd');
        $service = new MyService($this->connection);
        self::assertTrue($service->login('bob', 'p4ssw0rd'));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文