在PHPUnit中实现给定接口的模拟对象上的未定义方法? [英] Undefined method on mock object implementing a given interface in PHPUnit?

查看:97
本文介绍了在PHPUnit中实现给定接口的模拟对象上的未定义方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是单元测试和PHPUnit的新手.

I'm new to unit testing and PHPUnit.

我需要一个具有完全控制权的模拟程序,以实现ConfigurationInterface接口.测试对象是ReportEventParamConverter对象,测试必须检查我的对象和界面之间的交互.

I need a mock, on which I have a full control, implementing ConfigurationInterface interface. Test subject is ReportEventParamConverter object and test must check the interaction between my object and the interface.

ReportEventParamConverter对象(此处已简化):

ReportEventParamConverter object (here simplified):

class ReportEventParamConverter implements ParamConverterInterface
{
    /**
     * @param Request $request
     * @param ConfigurationInterface $configuration
     */
    function apply(Request $request, ConfigurationInterface $configuration)
    {
        $request->attributes->set($configuration->getName(), $reportEvent);
    }

    /**
     * @param ConfigurationInterface $configuration
     * @return bool
     */
    function supports(ConfigurationInterface $configuration)
    {
        return 'My\Namespaced\Class' === $configuration->getClass();
    }
}

这是我尝试模拟界面的方式:

And this is the way I'm trying to mock the interface:

$cls = 'Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface';
$mock = $this->getMock($mockCls);

我需要模拟两种方法的返回值:getClass()getName().例如:

I need to simulate the returned values for two methods: getClass() and getName(). For example:

$mock->expects($this->any())
    ->method('getClass')
    ->will($this->returnValue('Some\Other\Class'))
;

当我创建一个新的ReportEventParamConverter并测试supports()方法时,出现以下PHPUnit错误:

When i create a new ReportEventParamConverter and test supports() method, i get the following PHPUnit error:

致命错误:调用未定义的方法 Mock_ConfigurationInterface_21e9dccf :: getClass().

Fatal error: Call to undefined method Mock_ConfigurationInterface_21e9dccf::getClass().

$converter = new ReportEventParamConverter();
$this->assertFalse($converter->supports($mock));

推荐答案

这是因为ConfigurationInterface中没有"getClass"方法的声明.该接口中唯一的声明是方法"getAliasName".

It's because there is no declaration of "getClass" method in ConfigurationInterface. The only declaration in this interface is method "getAliasName".

您需要做的就是告诉模拟对象您将使用哪种方法:

All you need is to tell the mock what methods you will be stubing:

$cls = 'Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface';
$mock = $this->getMock($cls, array('getClass', 'getAliasName'));

请注意,没有"getClass"声明,但是您也可以存根/模拟不存在的方法.因此,您可以模拟它:

Notice that there is no "getClass" declaration but you can stub/mock non existing method as well. Therefor you can mock it:

$mock->expects($this->any())
    ->method('getClass')
    ->will($this->returnValue('Some\Other\Class'));

但是除此之外,您还需要模拟"getAliasName"方法,只要它是接口的方法或抽象方法,并且必须实现"即可.例如:

But in addtion you need to mock "getAliasName" method as well as long as it's interface's method or abstract one and it has to be "implemented". Eg.:

$mock->expects($this->any())
   ->method('getAliasName')
   ->will($this->returnValue('SomeValue'));

这篇关于在PHPUnit中实现给定接口的模拟对象上的未定义方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆