What is the difference between overload and alias in Mockery?(Mockery 中的重载和别名有什么区别?)
问题描述
我不熟悉使用 Mockery 并与术语 alias 和  混淆>重载.谁能给我解释一下什么时候用哪个?
I am new to using Mockery and confused with the terminology alias and overload. Can anyone please explain to me when to use which?
推荐答案
Overload 用于创建实例模拟".当创建一个类的新实例时,这将拦截"并且将使用模拟.例如,如果要测试此代码:
Overload is used to create an "instance mock". This will "intercept" when a new instance of a class is created and the mock will be used instead. For example if this code is to be tested: 
class ClassToTest {
    public function methodToTest()
    {
        $myClass = new MyClass();
        $result = $myClass->someMethod();
        return $result;
    }
}
您将使用 overload 创建一个实例模拟,并像这样定义期望:
You would create an instance mock using overload and define the expectations like this:
 public function testMethodToTest()
 {
     $mock = Mockery::mock('overload:MyClass');
     $mock->shouldreceive('someMethod')->andReturn('someResult');
     $classToTest = new ClassToTest();
     $result = $classToTest->methodToTest();
     $this->assertEquals('someResult', $result);
 }
Alias 用于模拟公共静态方法.例如,如果要测试此代码:
Alias is used to mock public static methods. For example if this code is to be tested:
class ClassToTest {
    public function methodToTest()
    {
        return MyClass::someStaticMethod();
    }
}
您将使用 alias 创建一个别名模拟,并像这样定义期望:
You would create an alias mock using alias and define the expectations like this:
public function testNewMethodToTest()
{
    $mock = Mockery::mock('alias:MyClass');
    $mock->shouldreceive('someStaticMethod')->andReturn('someResult');
    $classToTest = new ClassToTest();
    $result = $classToTest->methodToTest();
    $this->assertEquals('someResult', $result);
}
                        这篇关于Mockery 中的重载和别名有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Mockery 中的重载和别名有什么区别?
				
        
 
            
        - 带有通配符的 Laravel 验证器 2021-01-01
 - SoapClient 设置自定义 HTTP Header 2021-01-01
 - 从 PHP 中的输入表单获取日期 2022-01-01
 - PHP Count 布尔数组中真值的数量 2021-01-01
 - 正确分离 PHP 中的逻辑/样式 2021-01-01
 - Laravel 仓库 2022-01-01
 - Mod使用GET变量将子域重写为PHP 2021-01-01
 - 如何定位 php.ini 文件 (xampp) 2022-01-01
 - 没有作曲家的 PSR4 自动加载 2022-01-01
 - Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
 
				
				
				
				