Partial mocks allow you to mock some of the methods of a class while keeping the rest intact. Thus, you keep your original object, not a mock object, and you are still able to write your test methods in isolation. Partial mocking can be performed on both static and instance calls.
Note |
|---|
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. |
In the further examples we will use the following sample class to test:
CopyC#
public class Foo
{
public static int FooStaticProp { get; set; }
public int Echo(int arg1)
{
return default(int);
}
public void Execute()
{
throw new NotImplementedException();
}
}
CopyVB
Public Class Foo
Public Shared Property FooStaticProp() As Integer
Get
Return m_FooStaticProp
End Get
Set(value As Integer)
m_FooStaticProp = value
End Set
End Property
Private Shared m_FooStaticProp As Integer
Public Function Echo(arg1 As Integer) As Integer
Return 0
End Function
Public Sub Execute()
Throw New NotImplementedException()
End Sub
End Class
Partially Mock Instance Calls
In the following example we use a non mocked instance, but we call a method which is arranged.
CopyC#
[TestMethod]
public void ShouldMockInstanceCallPartially()
{
Foo foo = new Foo();
Mock.Arrange(() => foo.Echo(Arg.IsAny<int>())).Returns((int arg) => arg);
int actual = foo.Echo(10);
Assert.AreEqual(10, actual);
}
CopyVB
<TestMethod>
Public Sub ShouldMockInstanceCallPartially()
Dim foo As New Foo()
Mock.Arrange(Function() foo.Echo(Arg.IsAny(Of Integer)())).Returns(Function(arg__1 As Integer) arg__1)
Dim actual As Integer = foo.Echo(10)
Assert.AreEqual(10, actual)
End SubWith partial mocks we are still able to arrange a method call even when we don't use a mock object. Running the above test would pass.
Assert Partial Calls
While not using a mock object in partial mocking you can still assert that a specific call has been made during the executing of a test. In order to do that we need to pass the lambda of the method that we are expecting.
CopyC#
[TestMethod]
public void ShouldAssertCallsPartially()
{
Foo foo = new Foo();
Mock.Arrange(() => foo.Echo(Arg.IsAny<int>())).Returns((int arg) => arg);
foo.Echo(10);
foo.Echo(10);
Mock.Assert(() => foo.Echo(10), Occurs.Exactly(2));
}
CopyVB
<TestMethod>
Public Sub ShouldAssertCallsPartially()
Dim foo As New Foo()
Mock.Arrange(Function() foo.Echo(Arg.IsAny(Of Integer)())).Returns(Function(arg__1 As Integer) arg__1)
foo.Echo(10)
foo.Echo(10)
Mock.Assert(Function() foo.Echo(10), Occurs.Exactly(2))
End Sub
In the Assert section we make sure that the foo.Echo method has been called exactly two times by passing 10 as an argument. It is not required to enter a specific argument - you can use a matcher instead.
Arrange Static Calls
Another common usage of partial mocks is to arrange a call to a static method/property of a class.
CopyC#
[TestMethod]
public void ShouldArrangeStaticCallPartially()
{
Mock.Arrange(() => Foo.FooStaticProp).Returns(10);
int actual = Foo.FooStaticProp;
Assert.AreEqual(10, actual);
}
CopyVB
<TestMethod>
Public Sub ShouldArrangeStaticCallPartially()
Mock.Arrange(Function() Foo.FooStaticProp).Returns(10)
Dim actual As Integer = Foo.FooStaticProp
Assert.AreEqual(10, actual)
End SubHere we arrange that the static Foo.FooStaticProp property should return 10.
See Also
Other Resources
[d0e29c40-c54c-4994-a030-38855ba89cc6]