New to Telerik JustMock? Download free 30-day trial

When

Applied, the When clause enables the arrangement only if certain conditions are satisfied. It specifies what must be true (along with any argument matcher, or IgnoreInstance() or IgnoreArguments() clauses used on the arrangement) for that arrangement to be executed and its expectations updated. To understand how to use the When method, check the next examples:

How It Works

Let's assume we have the following interface:

public interface IFoo 
{ 
    bool IsCalled(); 
    string Prop { get; set; } 
} 
Public Interface IFoo 
    Function IsCalled() As Boolean 
 
    Property Prop() As String 
End Interface 

Using JustMock, we can set a certain arrangement to be used only if another expectations are met. The below test method is a demonstration of this. The IsCalled function is arranged to return true only if Prop is equal to "test".

[TestMethod] 
public void IsCalled_ShouldReturnTrue_WithMockedDependencies() 
{ 
    // Arrange 
    var foo = Mock.Create<IFoo>(); 
 
    Mock.Arrange(() => foo.Prop).Returns("test"); 
    Mock.Arrange(() => foo.IsCalled()).When(() => foo.Prop == "test").Returns(true); 
 
    // Assert 
    Assert.IsTrue(foo.IsCalled()); 
} 
<TestMethod> _ 
Public Sub IsCalled_ShouldReturnTrue_WithMockedDependencies() 
    ' Arrange 
    Dim foo = Mock.Create(Of IFoo)() 
 
    Mock.Arrange(Function() foo.Prop).Returns("test") 
    Mock.Arrange(Function() foo.IsCalled()).When(Function() foo.Prop = "test").Returns(True) 
 
    ' Assert 
    Assert.IsTrue(foo.IsCalled()) 
End Sub 

How It Helps

There are situations where you need to make an arrangement that will only work when a certain condition is true. If the condition is related to that member's input arguments, you can use Matchers. However, if the condition is not related to them, you can achieve the desired behavior by using the When method.

Look at the following interface:

public interface IRequest 
{ 
    string Method { get; set; } 
    string GetResponse(); 
} 
Public Interface IRequest 
    Property Method() As String 
    Function GetResponse() As String 
End Interface 

In the test, we will check if GetResponse() has been called once when Method == "GET" and once more when Method == "POST". Note that, the Method property is unrelated to the GetResponse() method.

[TestMethod] 
public void ShouldAssertThatSUTCallsGetResponseWithBothGetAndPostMethods() 
{ 
    // Arrange 
    var mock = Mock.Create<IRequest>(); 
 
    Mock.Arrange(() => mock.GetResponse()).When(() => mock.Method == "GET").OccursOnce(); 
    Mock.Arrange(() => mock.GetResponse()).When(() => mock.Method == "POST").OccursOnce(); 
 
    // Act 
    mock.Method = "GET"; 
    mock.GetResponse(); 
 
    mock.Method = "POST"; 
    mock.GetResponse(); 
 
    // Assert 
    Mock.Assert(mock); 
} 
<TestMethod> _ 
Public Sub ShouldAssertThatSUTCallsGetResponseWithBothGetAndPostMethods() 
    ' Arrange 
    Dim requestMock = Mock.Create(Of IRequest)() 
 
    Mock.Arrange(Function() requestMock.GetResponse()).When(Function() requestMock.Method = "GET").OccursOnce() 
    Mock.Arrange(Function() requestMock.GetResponse()).When(Function() requestMock.Method = "POST").OccursOnce() 
 
    ' Act 
    requestMock.Method = "GET" 
    requestMock.GetResponse() 
 
    requestMock.Method = "POST" 
    requestMock.GetResponse() 
 
    ' Assert 
    Mock.Assert(requestMock) 
End Sub 

See Also

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