This my the code, and it's working normally, but in test unit, it is not working ? he said that he doesn't have the create method.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$userModel = $objectManager->create('Magento\User\Model\User');
$userModel->loadByUsername("bbbb");
$role_id_under_selected_user = $userModel->loadByUsername("bbbb")->getRoles();
if ($userModel->getData('password') == NULL || empty($role_id_under_selected_user)) {
return false;
} else {
return true;
}
Thanks for your help.
-
I tried to use this $objectManager = new ObjectManager($this); instead of $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); but it's didn't works too.user3092453– user30924532017年02月19日 15:06:20 +00:00Commented Feb 19, 2017 at 15:06
-
Thanks, but the question is ? Can I check if "bbbb" user is existed or not ? without objectManager... because objectManager is not working for me...user3092453– user30924532017年02月21日 14:53:47 +00:00Commented Feb 21, 2017 at 14:53
1 Answer 1
Since you do not show the unit test code, I can only speculate, but it looks like you have an instance of the unit test object manager \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - it works differently and does not have a create() method. Instead it has (amongst others) a methods getObject() which gives you a new instance of the requested class with all dependencies mocked
It's recommended to neither use the unit object manager nor the real object manager in unit tests!1
Here's a statement from a Magento 2 core developer:
When M2 development started all classes use Mage::* methods and we start increase coverage with unit test. But when we add new constructor injection many test start fails. So, we decide to introduce helper class to avoid the unneeded work.
But now constructor dependencies are stabilized, so helper is not need in most cases
Source: https://magento.stackexchange.com/a/140433/243
Instead, instantiate the object under test with new and instantiate or stub/mock all dependencies explicitly.
In the production code (i.e. not the test), you should not use the object manager either, but instead use dependency injection and factories. If your code needs to instantiate a new User object, add \Magento\User\Api\Data\UserInterfaceFactory $userFactory as constructor parameter, and then replace
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$userModel = $objectManager->create('Magento\User\Model\User');
with
$userModel = $this->userFactory->create();
If you update the question with test code and maybe some more context, I can give you an example for the test as well.
1) Integration tests are a different topic, there you should use the real object manager to test that your DI configuration works correctly.
Update
From the question title, I guess you actually are writing an integration test, i.e. during the test your code operates on the database and you want to check if a user has been saved as expected.
In that case, the following works perfectly fine:
Get object manager from test framework bootstrap:
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()Get object manager directly:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
But your test should be executed as integration test, not as unit test. More on that here: Creating Integration Tests for Magento 2 Modules
-
Thanks @Fabian Schmengler, It's still doesn't works for me :/, May you write me a simple and short code for it.. with the constructor and the setup function and the testFunction ... 20-40 line codes how much it's takes.. my code is very simple, and from the begining... I will be more than happy that you will help me and other which stuck with this problem...user3092453– user30924532017年02月21日 10:09:27 +00:00Commented Feb 21, 2017 at 10:09
-
class bbbTest extends \PHPUnit_Framework_TestCase this is my class for example...user3092453– user30924532017年02月21日 10:12:57 +00:00Commented Feb 21, 2017 at 10:12
-
No, but if you update your question with a simple and short test code to reproduce your issue (minimum working example), I am willing to help fix itFabian Schmengler– Fabian Schmengler2017年02月21日 11:26:43 +00:00Commented Feb 21, 2017 at 11:26
-
I add code in the end... May you help me.. Thanks..user3092453– user30924532017年02月21日 13:14:32 +00:00Commented Feb 21, 2017 at 13:14
Explore related questions
See similar questions with these tags.