cancel
Showing results for 
Search instead for 
Did you mean: 

Integration Tests - Custom Model Fixtures

Integration Tests - Custom Model Fixtures

I tried to create a new model, different from what Magento has and I was unable to create a fixture for that entity when I wanted to create an integration test for a method. I also tried to create a fixture for an out-of-the-box model (Catalog Product) and I was able to access that entity in the test method, so I was able to assert properties.

 

I am using:

$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

to create new objects. This works fine for core classes like: 

->create(\Magento\Catalog\Model\Product::class)

But when I try to use it on a custom made module the data is not saved and I cannot load it inside my test. Is there an extra step or something we have to take in order to achieve the desired outcome?

 

3 Comments
philbirnie
Senior Member

Aretha -- having the same issue.  Were you able to come up with a solution?  

philbirnie
Senior Member

Aretha,

 

Fixed it in my case: 

 

The problem was that I was setting the idFieldName (see your resource model) of my particular model.

As a result, when saving the object (ref. below to AbstractDb.php),  $this->isObjectNotNew evaluates to TRUE because the idFieldName was set and the new object is not saved (Magento attempts to update the object instead):

 

public function save(\Magento\Framework\Model\AbstractModel $object)
{
    if ($object->isDeleted()) {
        return $this->delete($object);
    }

    $this->beginTransaction();

    try {
            ...

            if ($this->isObjectNotNew($object)) {
                $this->updateObject($object);
            } else {
                $this->saveNewObject($object);
            }

            ...

    return $this;
}
Aretha
Senior Member

Hi philbirnie,

 

So what you did is to just not set the id on the object?

How do you use it after that from your test method, don't you load it by id?

 

From what I saw in core they only save and load on category and product by id.