I am looking to write basic unit tests for some helper functions that I am working on for a Magento 2 module. I am able to execute my custom module unit tests but I am getting the following error for the first test.
When I execute the tests for my module I get the following error:
Call to a member function getBaseUrl() on null
My Helper code is below: I will be adding quite a few functions to it later and the function its self returns the correct result if I call it else where in my code, I am not able to get the test working.
namespace MyModuleSpace\MyModule\Helper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\StoreManagerInterface;
class Data extends \Magento\Framework\App\Helper\AbstractHelper {
/**
* Attribute to hold storeManager Object
*
* @var object
*/
protected $storeManager;
/**
* _construct
*
* @param Context $context
* @param StoreManagerInterface $storeManager
*/
public function __construct(Context $context, StoreManagerInterface $storeManager) {
$this->storeManager = $storeManager;
parent::__construct($context);
}
/**
* No params needed. Returns base site URL.
*
* @return string
*/
public function getBaseURL() {
return $this->storeManager->getStore()->getBaseUrl();
}
}
My Unit Test code is
namespace MyModuleSpace\MyModule\Test\Unit\Helper;
//class HelperTest extends \PHPUnit_Framework_TestCase { // not usable as it is not supported
class HelperTest extends \PHPUnit\Framework\TestCase {
protected $helper;
/**
* Sets up the Mock (reflection) objects and properties for tests...
*
*/
protected function setUp() {
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->helper = $objectManager->getObject('MyModuleSpace\MyModule\Helper\Data');
}
/**
* Run unit tests for getBaseURL()
*/
public function testGetBaseURL() {
$baseUrl = $this->helper->getBaseUrl();
// Test we are returning a string...
$this->assertTrue(is_string($baseUrl));
}
}
I am not sure what I am doing wrong but any help would be appreciated.
-
have you found a solution?, If yes, can you please tell me how.Sanjay Gohil– Sanjay Gohil2019年07月02日 06:55:12 +00:00Commented Jul 2, 2019 at 6:55
1 Answer 1
It's bad practise to use ObjecManager, so thats should be improved first. Use your construct to instantiate the classes that you need, including the helper.
-
I agree, I have found a better approach, just got to figure out how to close this question. Followed a bad example from some research I had...Baber– Baber2018年01月09日 14:16:10 +00:00Commented Jan 9, 2018 at 14:16
-
To close a question, you can simply accept an answer.BBQ.– BBQ.2018年01月09日 14:27:25 +00:00Commented Jan 9, 2018 at 14:27
-
Use of this object manager in unit tests is totally acceptableShapeshifter– Shapeshifter2020年03月17日 13:49:59 +00:00Commented Mar 17, 2020 at 13:49
-
as @Shapeshifter said there is not need to inject classes with a constructor in unit tests and your are not answering his questionopen-ecommerce.org– open-ecommerce.org2020年05月27日 11:31:55 +00:00Commented May 27, 2020 at 11:31