What is the difference between overload and alias in Mockery?

前端 未结 2 961
盖世英雄少女心
盖世英雄少女心 2021-01-02 12:08

I am new to using Mockery and confused with the terminology alias and overload. Can anyone please explain to me when to use which?

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-02 12:36

    I found this: https://github.com/padraic/mockery-docs/blob/master/reference/startup_methods.rst

    $mock = \Mockery::mock('alias:MyNamespace\MyClass');
    

    Prefixing the valid name of a class (which is NOT currently loaded) with "alias:" will generate an "alias mock". Alias mocks create a class alias with the given classname to stdClass and are generally used to enable the mocking of public static methods. Expectations set on the new mock object which refer to static methods will be used by all static calls to this class.

    $mock = \Mockery::mock('overload:MyNamespace\MyClass');
    

    Prefixing the valid name of a class (which is NOT currently loaded) with "overload:" will generate an alias mock (as with "alias:") except that created new instances of that class will import any expectations set on the origin mock ($mock). The origin mock is never verified since it's used an expectation store for new instances. For this purpose we use the term "instance mock" to differentiate it from the simpler "alias mock".

    So to me it seems that overload does the same as alias with the difference that it also imports expectations from the origin mock.

提交回复
热议问题