在某些情况下如何告诉 mock 调用原始方法? [英] How do I tell mock to call the original method under certain circumstances?

查看:57
本文介绍了在某些情况下如何告诉 mock 调用原始方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个课程想在稍后的测试中模拟:

I have a class that would want to mock later in a test:

class Original {
  function calculate($a) { ... }
}

测试中:

$mock = $this->getMock('Original',['calculate']);
$mock->expcets($this->any())
     ->will($this->returnValueMap([ 
      [1,'ABC'],
      [2,'BCD']
      // somehow tell $mock to call the original calculate() here
);

我想告诉 $mock 在某些条件下回退到原始的 calculate(),例如,如果参数与任何提供的检查不匹配.正如我从阅读 PhpUnit 代码中了解到的,如果未找到匹配项,调用程序只会返回 null :-?

I want to tell $mock to fall back to the original calculate() under certain conditions, for example if arguments do not match any of provided checks. As I understand from reading PhpUnit code the invoker just returns null if no match is found :-?

推荐答案

$mock = $this->getMockBuilder(Original::class)
            ->setMethodsExcept(['calculate'])->disableOriginalConstructor()
            ->getMock();

这将调用原始的 calculate 函数,无论如何.

This will call the original calculate function, no matter what.

这篇关于在某些情况下如何告诉 mock 调用原始方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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