mardi 27 février 2018

Mock an interface instantiation in a function call argument list with phpunit

I'm bit new to phpunit .I was trying to cover this function .

public function add(AddressInterface $address) : array
{
    $this->data[] = $address;
    return $this->data;
}

My test function is as follows

public function testAdd()
{
    $collection = new AddressCollection();
    $collection->add($this->createMock(AddressInterface::class));
}

and the namespaces are same. But whenever I try running the phpunit , I'm getting this error . TypeError: Argument 1 passed to ValidateAddress\Model\AddressCollection::add() must be an instance of AddressInterface, instance of Mock_AddressInterface_65a1b00b given, called in validateaddress/tests/Model/AddressCollectionTest.php

Any idea why this is happening? The mocked instance cannot replace the instance of AddressInterface in original add() function version. Any help is appreciated!

Aucun commentaire:

Enregistrer un commentaire