New to Telerik JustMock? Download free 30-day trial

Do Nothing

The DoNothing method is used to arrange that a call to a method or property should be ignored. When using DoNothing, all the logic inside the arranged method or property body is skipped and nothing happens when you call it.

We will use the following interface for the examples in this article:

Sample setup

public interface IFoo 
{ 
    int Bar { get; set; } 
    void VoidCall(); 
} 

Skip the Logic of a Method

Before describing the usage of the DoNothing method, let's first see an example to illustrate it.

Example 1: Sample usage of DoNothing on a void method

[TestMethod] 
public void SimpleExampleWithDoNothing() 
{ 
    // Arrange 
    var foo = Mock.Create<IFoo>(); 
 
    Mock.Arrange(() => foo.VoidCall()).DoNothing().MustBeCalled(); 
 
    // Act 
    foo.VoidCall(); 
 
    // Assert 
    Mock.Assert(foo); 
} 

In the example, we mark foo.VoidCall() with DoNothing and MustBeCalled. In this way, we indicate that a call to foo.VoidCall must be ignored and nothing should happen, but still the method should be called during the execution of the test.

You can use DoNothing with non-void calls as well. For the example we will be using the following Foo class:

Sample setup

public class Foo 
{ 
    public int Echo(int num) 
    { 
        return num; 
    } 
} 

Example 2: Sample usage of DoNothing with non-void method

[TestMethod] 
public void DoNothingWithNonPublicCalls() 
{ 
    // Arrange 
    var foo = Mock.Create<Foo>(); 
 
    Mock.Arrange(() => foo.Echo(Arg.AnyInt)).DoNothing(); 
 
    // Act 
    foo.Echo(10); 
} 

Skip the Logic of a Property Setter

DoNothing can be also used on a call to a property set.

Example 4: Usage of DoNothing on property setter

[TestMethod] 
public void AssertDoNothingOnPropertySet() 
{ 
    // Arrange 
    var foo = Mock.Create<IFoo>(); 
 
    Mock.ArrangeSet(() => foo.Bar = 1).DoNothing().MustBeCalled(); 
 
    // Act 
    foo.Bar = 1; 
 
    // Assert 
    Mock.Assert(foo); 
}