Note that there are some explanatory texts on larger screens.

plurals
  1. POMagento - UnitTests - Mock Objects
    primarykey
    data
    text
    <p>I am writing some tests for a Magento module, using <a href="http://www.ecomdev.org/2011/02/10/new-version-of-ecomdev_phpunit-0-1-2-extension-for-magento.html" rel="nofollow">Ivan Chepurnyi's extension</a>, and I'm having trouble using the mock objects.<br> Here is the class:</p> <pre><code>&lt;?php class Namespace_Module_Block_Class extends Mage_Core_Block_Template { private $_salesCollection; public function __construct() { $this-&gt;_salesCollection = Mage::getModel('module/classA')-&gt;getCollection() -&gt;addFieldToFilter('id', $this-&gt;_getId()); } public function _getId() { return Mage::getModel('module/classB')-&gt;getId();//session params } public function getSalesTotalNumber() { return $this-&gt;_salesCollection-&gt;count(); } } </code></pre> <p>The method I'm trying to test is <strong>getSalesTotalNumber()</strong>.<br> And here is the test:</p> <pre><code>&lt;?php class Namespace_Module_Test_Block_Class extends EcomDev_PHPUnit_Test_Case { private $_mock; public function setUp() { $this-&gt;_mock = $this-&gt;getMock('Namespace_Module_Block_Class', array('_getId') ); $this-&gt;_mock-&gt;expects($this-&gt;any()) -&gt;method('_getId') -&gt;will($this-&gt;returnValue(1024)); parent::setUp(); } /** * @test * @loadFixture * @loadExpectation */ public function testSalesTotalNumber() { $actual = $this-&gt;_mock-&gt;getSalesTotalValue(); $expected = $this-&gt;_getExpectations()-&gt;getSalesTotalNumber(); $this-&gt;assertEquals($expected, $actual); } } </code></pre> <p>As you can see, what I want to do is overwrite the <strong>_getId()</strong> method so that it returns an id which match the id in the fixture and so load the collection. But it doesn't work :-(. </p> <p>In my test, if I echo <code>$this-&gt;_mock-&gt;_getId()</code> it returns the correct Id (1024). But in the <code>__construct()</code> of my class <code>$this-&gt;_getId()</code> returns null, which is the <em>expected</em> 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 <strong>_getId()</strong> method isn't <em>mocked</em> by my test case. </p> <p>Any help will be highly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload