我有以下课程
groupBox1.Width = Width / 3 - 20;
listBoxPlayer1.Left = 6;
listBoxPlayer1.Width = groupBox1.Width / 2 - GAP / 3;
listBoxKeepers1.Width = listBoxPlayer1.Width;
labelK1.Left = groupBox1.Width / 2 + SMALL_GAP;
listBoxKeepers1.Left = labelK1.Left;
SomeInterface包含以下内容:
namespace MyApp;
use MyApp\SomeInterface;
class MyClass
{
public function __construct(SomeInterface $s)
{
//Some Logic here
}
//Another methods implemented There
}
我想在我的phpunit测试类上创建一个模拟:
namespace MyApp
interface SomeInterface
{
/**
* @return SomeObject
*/
public function someMethodToImpement();
}
但是一旦运行它,我就会收到错误消息:
Tests \ MyApp \ MyClassTest :: someTest TypeError:传递给MyApp \ MyClass :: __ construct()的参数1必须实现接口MyApp \ SomeInterface,PHPUnit \ Framework \ MockObject \ Builder \ InvocationMocker的实例,在/home/vagrant/code/tests/MyApp/MyClassTest.php中调用在线
您知道为什么会发生这种情况以及如何实际创建模拟接口吗?
答案 0 :(得分:4)
而不是通过构建构造
$mockInterface=$this->createMock(SomeInterface::class)
->method('someMethodToImpement')->will($this->returnValue($fakeClass));
将其拆分为单独的行:
$mockInterface=$this->createMock(SomeInterface::class);
$mockInterface->method('someMethodToImpement')->will($this->returnValue($fakeClass));
并且会像魅力一样工作。
答案 1 :(得分:0)
我遇到了类似的问题。我通过将这些接口添加为另一个mock()
参数
class Product implements PriceInterface, ProductDataInterface {
// ...
}
测试:
// throws error
$product = Mockery::mock(Product::class);
// works fine
$product = Mockery::mock(Product::class, 'PriceInterface, ProductDataInterface');