Mocking Ref Returns
Ref return is one of the advanced features supported in Telerik® JustMock. It gives you ability to arrange and verify expectations of public and non-public instance and static ref return methods and properties in elevated mode.
Ref return is a feature specific for C# language and have limited support in other CLR languages. To use ref return mocking, you have to enable profiler for elevated mode. You can refer to this topic for more information about advanced features and how to enable them.
ClassUsingRefReturns
sample class is used below to figure out different ref return use cases. Here is its implementation:
Mocking Public Ref Return Members
The typical example of mocking public instance method could be illustrated as following:
In this example, there is an arrangement to fake ref return and verify occurrences of instance method using Mock.Arrange
. The interesting part of this arrangement is to provide a value which can be considered as ref return. JustMock introduces a dedicated delegate type FuncExpectation<TReturn>.RefDelegate
for this purpose and LocalRef
helper class which simplifies ref return arrangements and verifications.
Important
Ref return fakes are implemented via delegates.
LocalRef
helper class hides a lot of implementation details and makes your test code easy to maintain.
Another common use case is mocking of public static member. The next example shows how to achieve that:
Mocking Non-public Ref Return Members
Non-public API provides several dedicated interfaces for mocking ref returns and accessing members following a well-known JustMock approach. Let's see how this looks like in action:
The example demonstrates ref return faking and occurrence verification handled by INonPublicRefReturnExpectation
interface. Accessing non-public members is supported via IPrivateRefReturnAccessor
interface as a part of PrivateAccessor
. The next example gives an idea of how you can mock non-public ref return static members: