1

I create simple unit test with object manager with class name

$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this)

and object Name ModuleList like

$moduleList = $objectManager->getObject(ModuleList::class);

    print_r($moduleList->getAll());

but not getting all module list

this my unitTest file give below

namespace NameSpaceModule\Test\Test\Unit;

use Magento\Framework\Module\ModuleList;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\TestCase;

class SampletTest extends TestCase
{
    private $moduleName = 'NameSpaceModule_Test';


    public function testTheModuleIsConfiguredAndEnabled(){
        /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager $objectManager */
        $objectManager = new ObjectManager($this);

        /** @var ModuleList $moduleList */
        $moduleList = $objectManager->getObject(ModuleList::class);

        print_r($moduleList->getAll());
    }
}

1 Answer 1

1

Unit tests are supposed to test your code in isolation. In the Magento unit test suite, the application is not instantiated, that means there is no database, no module configuration etc. Even using the object manager is not recommended. The unit test object manager helper was only introduced for old legacy core code.

What you are trying to test would be an integration test, not a unit test. You test how the module is integrated in Magento. In integration test the application is instantiated, with a test database and everything.

Just one more remark about your specific test: in integration tests, all modules are enabled automatically. So you will effectively just test if the module exists and can be enabled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.