Static mocking is one of the advanced features supported in Telerik JustMock.
It allows you to fake static constructors, methods and properties calls, set expectations and verify results using the AAA principle.
Whether you mock static or instance calls there isn't much difference in the way your organize and write your tests.
We can divide static mocking into the following major parts:
- Static constructor mocking
- Static method mocking
- Extension methods mocking
This feature is available only in the commercial version of Telerik JustMock.
Refer to this topic to learn more about the differences between both the commercial and free versions of Telerik JustMock.
Further in this topic we will use the following sample code to illustrate how to mock static constructors, methods and properties.
Static Constructor Mocking
In the following example you will see how you can specify the behavior of the static constructor when the target type is mocked. The first thing you need to do is to set up the target type for mocking all static calls. You do this using one of the following calls to the Mock.SetupStatic method.
void Mock.SetupStatic( Type staticType );
void Mock.SetupStatic( Type targetType, Behavior behavior );
void Mock.SetupStatic( Type staticType, StaticConstructor staticConstructor );
void Mock.SetupStatic( Type targetType, Behavior behavior, StaticConstructor staticConstructor );
As you can see you have a number of choices. You always have to provide the type of the target class you want to set up for mocking. You can also specify the behavior of the mock. The default behavior is Behavior.Loose.
The StaticConstructor parameter defines the default behavior of the static constructor. You can choose from the following values:
The default value for this property is NonMocked
Let's see a complete example using the class Foo from the sample code in the beginning.
Here we have set up the static constructor mock of the target type Foo. Using the StaticConstructor parameter in the call to SetupStatic we have specified that we want to mock the call to the static constructor and therefore the call to the Foo.FooProp will not throw a NotImplementedException.
General Static Method Mocking
Let's start with the simplest example, namely how to mock the static Submit method. First, we need to call the following:
|Visual Basic ||Copy|
This call setups the Foo type for static mocking and prepares all the static methods as mockable.
This is actually the only difference between static and instance mocking. From now on you continue as if you are mocking instance methods.
|Visual Basic ||Copy|
In the Submit method implementation we throw an exception, but as we mocked the Foo class that exception should not be thrown.
Finally, we can assert that the method was actually called.
Mock.Assert(() => Foo.Submit());
|Visual Basic ||Copy|
With Mock.SetupStatic(typeof(Foo), StaticConstructor.Mocked); we setup that all static methods
from this class will me mocked. In certain cases you'd need to mock only methods that are setup with Mock.Arrange.
To achieve this you need to set the Behavior to Strict in the Mock.SetupStatic call.
Once we set the Behavior to Strict, only calls setup through arrange are mocked.
Other calls will throw a MockException.
Follows an example of mocking static method in F#:
member this.ShouldMockStaticCall() =
Mock.Arrange(fun ignore -> Foo.EchoStatic()).Returns(1);
The static EchoStatic method is arranged to return 1.
Mocking Static Property Get
You can also set up a static property get:
We replace the actual implementation of Foo.FooProp with called = true; and return 1.
After acting we verify that the method was actually called with return value 1.
Mocking Static Property Set
Now, let's mock a static property set. In the example below, we arrange the Foo.FooProp property.
We use DoInstead to set a local boolean variable to true once it is assigned 10.
After that, we verify that what we have expected actually happened in our test.
Go to Mock Properties topic to learn more about mocking properties.
Mocking Internal Static Call
Going further, you can mock internal methods like in the following example.
Here, calling FooInternal.DoIt should not throw an exeption as you are
allowed to setup static internal methods.
Mocking Static Class
In the demonstrated examples, the class itself that we mock is a non static class - only the methods
are static. To mock a static class you need to use the non generic version of the
Mock.SetupStatic method, i.e.
Mocking static members across threads
Mocking static members across all threads is an unsafe operation that may compromise the
stability of the testing framework. Arrangements on static members are valid only for the current thread by default.
To make an arrangement on a static member valid on all threads, add the .OnAllThreads() clause to the arrangement:
Mock.Arrange(() => DateTime.Now).Returns(new DateTime()).OnAllThreads();
Mocking Current HttpContext
Here is an example how to mock the current HTTP context.
We arrange a call to HttpContext.Current to set a local variable to true.
Note that, the original implementation of HttpContext.Current won`t be executed.
After acting we verify against our expectations.
Mocking Extension Methods
An extension method is a new language feature of C# starting with the 3.0 specifications,
as well as Visual Basic.NET starting with 9.0. Extension methods enable you to "add" methods to existing
types without creating a new derived type, recompiling, or otherwise modifying the original type.
Extension methods are a special kind of static methods, but they are called as if they were instance methods on
the extended type.
Mocking extension method is similar to mocking any instance methods. The only difference is that we don’t need
Mock.Create<T>() call to initialize the class for mocking as extension
mocking is by default partial.
Let's see an example of how to mock extension methods. Consider the following class:
And a class that contains extension methods for the Foo class:
Let's mock the Echo extension method.
First we create an instance of the Foo class. Notice that
we create a standard instance of the class, not a mock instance. Then we setup the call to Echo
through Arrange in the same way we do it for non static methods. Finally, we assert the return value as usual.