简体   繁体   中英

Mocking to multiple Interfaces

Is it possible to mock an object in a way that it fakes multiple interfaces implementations? (Note: I am using "padraic's mockery" https://github.com/padraic/mockery )

Suppose I have a class Mysql, which implements Db_Interface and Configurable_Interface, and which I need to mock just to be able to test another class. I want to create a mock with another name, not Mysql (because it could change or disappear in the future, that's why we use interfaces, right?), so I don't want to do Mockery::mock('Mysql').

I know I could create it like Mockery::mock('Db_Interface') and it would pass the instanceof Db_Interface check. But how can I make it pass the check for the other interface too?

@Gordon ok heres the code:

$m = Mockery::mock('Configurable_Interface');
var_dump($m instanceof Configurable_Interface); // true
var_dump($m instanceof Db_Interface); // false of course, since I don't know how to make a mock implement 2 interfaces

For anyone stumbling into this. In Mockery , you can call Mockery::mock('firstInterface, secondInterface'); to mock an object wich needs to implement multiple interfaces.

Source: Mockery README

$this->getMockBuilder(['A', 'B'])->getMock();

This thread comes out when the question is about phpspec !

So here is a way to do it with phpspec:

$prophecy = $this->prophesize(InterfaceA::class);
$prophecy->willImplement(InterfaceB::class);

👍

you need a stub. create a class which implements the 2 interfaces. all methods with "return null". than create a mock with this stub.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM