New to Telerik JustMock? Download free 30-day trial

Strict

By default Telerik® JustMock uses loose mocks (Behavior.RecursiveLoose) and allows you to call any method on a given type. No matter whether the method call is arranged or not, you are able to call it. However, you may have a case where you want to enable only arranged calls and to reject non-arranged calls. In such cases you need to set the Behavior to Strict when creating the mock. Another case where you may need to use strict mocks is when arranging calls to property setters.

Caution

If you try to call a method that is not arranged on a strict mock an exception will be thrown - StrictMockException.

Syntax

Mock.Create<T>(Behavior.Strict);

Examples

Arbitrary Calls Generate Exception in Strict Mocks

The next example shows the creation of a strict mock, without any further arrangements. Calling a non previously arranged member will result in throwing of a MockException.

public interface IFoo 
{ 
    void VoidCall(); 
} 
Public Interface IFoo 
    Sub VoidCall() 
End Interface 

[TestMethod] 
[ExpectedException(typeof(StrictMockException))] 
public void ArbitraryCallsShouldGenerateException() 
{ 
    //Arrange 
    var foo = Mock.Create<IFoo>(Behavior.Strict); 
 
    //Act 
    foo.VoidCall(); 
} 
<TestMethod> _ 
<ExpectedException(GetType(StrictMockException))> _ 
Public Sub ArbitraryCallsShouldGenerateException() 
    'Arrange 
    Dim foo = Mock.Create(Of IFoo)(Behavior.[Strict]) 
 
    'Act 
    foo.VoidCall() 
End Sub 

Testing If Functions Work in Isolation

Let assume we have the following class:

public class Foo 
{ 
    public void VoidMethod() 
    { 
        // Some logic here... 
    } 
    public void ExecuteVoidMethod() 
    { 
        this.VoidMethod(); 
    } 
 
    public string SomeStringFunctiond() 
    { 
        return default(string); 
    } 
 
    public int SomeIntFunctiond() 
    { 
        return default(int); 
    } 
} 
Public Class Foo 
    Public Sub VoidMethod() 
        ' Some logic here... 
    End Sub 
    Public Sub ExecuteVoidMethod() 
        Me.VoidMethod() 
    End Sub 
 
    Public Function SomeStringFunctiond() As String 
        Return Nothing 
    End Function 
 
    Public Function SomeIntFunctiond() As Integer 
        Return 0 
    End Function 
End Class 

Now, we want to test the ExecuteVoidMethod() function. The next test should throw a MockException if non-arranged member is called. In our case such member is the VoidMethod() function:

[TestMethod] 
[ExpectedException(typeof(StrictMockException))] 
public void ShouldAssertIfThereAreCallsToNonArrangedMembers() 
{ 
    // Arrange 
    var foo = Mock.Create<Foo>(Behavior.Strict); 
 
    Mock.Arrange(() => foo.ExecuteVoidMethod()).CallOriginal().OccursAtLeast(1); 
 
    // Act 
    foo.ExecuteVoidMethod(); 
} 
<TestMethod> _ 
<ExpectedException(GetType(StrictMockException))> _ 
Public Sub ShouldAssertIfThereAreCallsToNonArrangedMembers() 
    ' Arrange 
    Dim foo = Mock.Create(Of Foo)(Behavior.[Strict]) 
 
    Mock.Arrange(Sub() foo.ExecuteVoidMethod()).CallOriginal().OccursAtLeast(1) 
 
    ' Act 
    foo.ExecuteVoidMethod() 
End Sub 

The next example shows the basic usage of a strict mock. The test checks for the basic functions of the SUT in isolation:

[TestMethod] 
public void ShouldAssertIfThereAreNOArbitraryCalls() 
{ 
    // Arrange 
    var foo = Mock.Create<Foo>(Behavior.Strict); 
 
    Mock.Arrange(() => foo.VoidMethod()).OccursAtLeast(1); 
    Mock.Arrange(() => foo.ExecuteVoidMethod()).CallOriginal().OccursAtLeast(1); 
 
    // Act 
    foo.ExecuteVoidMethod(); 
 
    // Assert 
    Mock.Assert(foo); 
} 
<TestMethod> _ 
Public Sub ShouldAssertIfThereAreNOArbitraryCalls() 
    ' Arrange 
    Dim foo = Mock.Create(Of Foo)(Behavior.[Strict]) 
 
    Mock.Arrange(Sub() foo.VoidMethod()).OccursAtLeast(1) 
    Mock.Arrange(Sub() foo.ExecuteVoidMethod()).CallOriginal().OccursAtLeast(1) 
 
    ' Act 
    foo.ExecuteVoidMethod() 
 
    ' Assert 
    Mock.Assert(foo) 
End Sub 

First, we create the mock with Behavior.Strict. Then, we arrange the methods that should be invoked during the test execution. Finally, we assert against them.

See Also

In this article
Not finding the help you need? Improve this article