其他分享
首页 > 其他分享> > 嘲笑:被动部分模拟与默认模拟有何不同?

嘲笑:被动部分模拟与默认模拟有何不同?

作者:互联网

在此(很棒的)快速参考Mockery的最后几段中,作者解释了一些模拟的行为修饰符,这些修饰符不是默认的,但可能有用.这些包括makePartial()调用和shouldDeferMissing()调用.

这些与默认行为有何不同?当我创建一个模拟(Mockery :: mock(‘myClass’))且不添加任何方法期望时,据我所知,所有方法调用都转到父级(即MyClass)…

这是Mockery快速参考的最后一部分.

\Mockery::mock(‘MyClass’)->makePartial()

also

\Mockery::mock(‘MyClass’)->shouldDeferMissing()

Known as a Passive Partial Mock (not to be confused with real partial
mock objects discussed later), this form of mock object will defer all
methods not subject to an expectation to the parent class of the mock,
i.e. MyClass. Whereas the previous shouldIgnoreMissing() returned
null, this behaviour simply calls the parent’s matching method.

解决方法:

>完整的模拟对象要求必须进行所有方法调用
>真正的部分模拟对象将仅模拟指定的方法,并且您无法为非模拟方法设置期望.如果在创建模拟时未指定方法,它将延迟对该方法的所有调用到父类.
>被动部分模拟将延迟方法调用,而不期望模拟的父类.

后两者的区别在于,对于被动部分模拟,您无需在设置期望值之前指定要模拟的方法.当您设置期望值时,它将为您创建模拟方法.

标签:mocking,mockery,php
来源: https://codeday.me/bug/20191029/1958261.html