Magento - 单元测试 - 模拟对象

发布于 2024-11-01 04:57:01 字数 2093 浏览 1 评论 0 原文

我正在为 Magento 模块编写一些测试,使用 Ivan Chepurnyi 的扩展,并且我在使用模拟对象时遇到问题。
这是该类:

<?php
class Namespace_Module_Block_Class extends Mage_Core_Block_Template
{
    private $_salesCollection;

    public function __construct()
    {
        $this->_salesCollection = Mage::getModel('module/classA')->getCollection()
                                ->addFieldToFilter('id', $this->_getId());
    }

    public function _getId()
    {
        return Mage::getModel('module/classB')->getId();//session params
    }

    public function getSalesTotalNumber()
    {
        return $this->_salesCollection->count();
    }
}

我尝试测试的方法是 getSalesTotalNumber()
这是测试:

<?php
class Namespace_Module_Test_Block_Class extends EcomDev_PHPUnit_Test_Case
{
    private $_mock;

    public function setUp()
    {
        $this->_mock = $this->getMock('Namespace_Module_Block_Class',
                                        array('_getId')
                                      );
        $this->_mock->expects($this->any())
                    ->method('_getId')
                    ->will($this->returnValue(1024));

        parent::setUp();
    }

    /**
     * @test
     * @loadFixture
     * @loadExpectation
     */
    public function testSalesTotalNumber()
    {
        $actual = $this->_mock->getSalesTotalValue();
        $expected = $this->_getExpectations()->getSalesTotalNumber();

        $this->assertEquals($expected, $actual);
    }
}

如您所见,我想要做的是覆盖 _getId() 方法,以便它返回一个与夹具中的 id 匹配的 id,从而加载集合。但它不起作用:-(。

在我的测试中,如果我 echo $this->_mock->_getId() 它会返回正确的 Id (1024)。但是在 $this->_getId() 的 >__construct() 返回 null,这是测试期间的预期值(我的意思是,在测试期间不是会话,因此它无法获取对象的 Id,因为我将其存储在会话变量中),因此 _getId() 方法没有被我的测试模拟。 。

任何帮助将不胜感激

I am writing some tests for a Magento module, using Ivan Chepurnyi's extension, and I'm having trouble using the mock objects.
Here is the class:

<?php
class Namespace_Module_Block_Class extends Mage_Core_Block_Template
{
    private $_salesCollection;

    public function __construct()
    {
        $this->_salesCollection = Mage::getModel('module/classA')->getCollection()
                                ->addFieldToFilter('id', $this->_getId());
    }

    public function _getId()
    {
        return Mage::getModel('module/classB')->getId();//session params
    }

    public function getSalesTotalNumber()
    {
        return $this->_salesCollection->count();
    }
}

The method I'm trying to test is getSalesTotalNumber().
And here is the test:

<?php
class Namespace_Module_Test_Block_Class extends EcomDev_PHPUnit_Test_Case
{
    private $_mock;

    public function setUp()
    {
        $this->_mock = $this->getMock('Namespace_Module_Block_Class',
                                        array('_getId')
                                      );
        $this->_mock->expects($this->any())
                    ->method('_getId')
                    ->will($this->returnValue(1024));

        parent::setUp();
    }

    /**
     * @test
     * @loadFixture
     * @loadExpectation
     */
    public function testSalesTotalNumber()
    {
        $actual = $this->_mock->getSalesTotalValue();
        $expected = $this->_getExpectations()->getSalesTotalNumber();

        $this->assertEquals($expected, $actual);
    }
}

As you can see, what I want to do is overwrite the _getId() method so that it returns an id which match the id in the fixture and so load the collection. But it doesn't work :-(.

In my test, if I echo $this->_mock->_getId() it returns the correct Id (1024). But in the __construct() of my class $this->_getId() returns null, which is the expected value during testing (I mean, during testing there is no session, so it can't get the object's Id as I store it in a session variable). So the _getId() method isn't mocked by my test case.

Any help will be highly appreciated.

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

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

发布评论

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

评论(1

十秒萌定你 2024-11-08 04:57:01

所以我的问题不在模拟/测试中,而是在课堂上。
我已将 __construct() 的内容移至返回集合对象的受保护方法中。这就是我的班级现在的样子:

<?php
class Namespace_Module_Block_Class extends Mage_Core_Block_Template
{
    private $_salesCollection;

    protected function _getAffiliateSales()
    {
        if (is_null($this->_salesCollection)) {
            $affiliateId = $this->_getId();
            $this->_salesCollection = Mage::getModel('module/classA')
                                ->addFieldToFilter('id', $affiliateId);
        }
        return $this->_salesCollection;
    }

    public function _getId()
    {
        return Mage::getModel('module/classB')->getId();//session params
    }

    public function getSalesTotalNumber()
    {
        return $this->_getAffiliateSales()->count();
    }
}

So my problem was not in the mock/test but in the class.
I have moved the content of __construct() into a protected method which returns the collection object. That's how my class looks like now:

<?php
class Namespace_Module_Block_Class extends Mage_Core_Block_Template
{
    private $_salesCollection;

    protected function _getAffiliateSales()
    {
        if (is_null($this->_salesCollection)) {
            $affiliateId = $this->_getId();
            $this->_salesCollection = Mage::getModel('module/classA')
                                ->addFieldToFilter('id', $affiliateId);
        }
        return $this->_salesCollection;
    }

    public function _getId()
    {
        return Mage::getModel('module/classB')->getId();//session params
    }

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