1

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.

asked Jan 7, 2018 at 15:09
1
  • have you found a solution?, If yes, can you please tell me how. Commented Jul 2, 2019 at 6:55

1 Answer 1

-5

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.

answered Jan 7, 2018 at 15:59
4
  • 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... Commented Jan 9, 2018 at 14:16
  • To close a question, you can simply accept an answer. Commented Jan 9, 2018 at 14:27
  • Use of this object manager in unit tests is totally acceptable Commented 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 question Commented May 27, 2020 at 11:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.