testframework
testframework copied to clipboard
Cannot create simple mock object
I create simple test with creating mock object but this throw Exception.
public function testCreateSimpleMock()
{
$logger = $this->getMock('Logger');
$this->assertTrue(true);
}
Exception:
$ phpunit -c vendor/popov/magento-magmi-import/app/code/local/Popov/Magmi/Test/phpunit.xml
PHPUnit 5.7.5 by Sebastian Bergmann and contributors.
Runtime: PHP 7.0.8 with Xdebug 2.4.0
Configuration: D:\WebServer\git\art-market\vendor\popov\magento-magmi-import\app\code\local\Popov\Magmi\Test\phpunit.xml
E 1 / 1 (100%)
Time: 3.33 seconds, Memory: 16.00MB
There was 1 error:
1) Popov_Magmi_Test_Import_ImageTest::testCreateSimpleMock
Exception: Warning: include(Logger.php): failed to open stream: No such file or directory in D:\WebServer\git\art-market\htdocs\lib\Varien\Autoload.php on line 94
D:\WebServer\git\art-market\htdocs\app\code\core\Mage\Core\functions.php:245
D:\WebServer\git\art-market\vendor\digitalpianism\testframework\lib\DigitalPianism\TestFramework\Helper\Magento.php:24
D:\WebServer\git\art-market\htdocs\lib\Varien\Autoload.php:94
D:\WebServer\git\art-market\htdocs\lib\Varien\Autoload.php:94
D:\WebServer\git\art-market\vendor\popov\magento-magmi-import\app\code\local\Popov\Magmi\Test\Import\ImageTest.php:29
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
I reckon you need to provide the full path to the Logger
class when creating the mock. Which class are you trying to mock ?
I don't rely on real object, this is Fake only for test passing. I need create decoupled Unit Test with mocking everything from "outside world".
In real test I use Mocker library but it does not matter. I want to mock Product object and I create next code and this throw the same exception as above.
$simpleProductMock = m::mock('Product')
->shouldReceive('getResource')->andReturnSelf()->getMock()
->shouldReceive('getAttribute')->andReturnSelf()->getMock()
->shouldReceive('setStoreId')->with(Mage_Core_Model_App::ADMIN_STORE_ID)->getMock()
->shouldReceive('getAttributeText')->andReturn('red')->getMock()
->shouldReceive('getSku')->andReturn('SKU_PRODUCT_1')->getMock();
$catalogModelMock = m::mock('Catalog_Model_Product_Type_Configurable')
->shouldReceive('getUsedProducts')
->andReturn([$simpleProductMock]) // return first on first call and second on second
->getMock();
If you can provide working solution for this case I will very appreciate.