New to Telerik JustMock? Download free 30-day trial

JustMock Cheat Sheet

This cheat sheet is created to help you navigate faster around the JustMock API and reduce your learning curve. For further explanations and other scenarios please check the rest of the articles in this documentation.

Creating a mock object

Code Description
IFoo mock = Mock.Create(); Create a mock with default constructor
IFoo mock = Mock.Create(Constructor.NotMocked); Create a mock without mocking the constructor
IFoo mock = Mock.Create(foo => foo.Index = 5); Create a mock with predefined value
IFoo mock = Mock.Create(Behavior.RecursiveLoose); Create a mock with specific behavior

Mock behavior

You can control what the behavior of a mock will be when its members are accessed.

Code Description
IFoo mock = Mock.Create(); Recursive Loose (default) – returns default value for value type members and empty, non-null stubs for reference type (including strings) and collection members.
IFoo mock = Mock.Create(Behavior.RecursiveLoose);
IFoo mock = Mock.Create(Behavior.Loose); Loose – returns default value for value type members, null for reference type members and empty non-null stubs for collection member.
IFoo mock = Mock.Create(Behavior.Strict); Strict – behaves according to explicit arrangements, throws if there is no existing arrangement to the corresponding call.
IFoo mock = Mock.Create(Behavior.CallOriginal); Call original – behaves like original implementation except explicit arrangements.

Arrangements

Those are some of the most common arrangements.

Code Description
Mock.Arrange(() => mock.Echo(Arg.AnyInt)).DoNothing(); Ignore method call.
int expectedResult = 0;
Mock.Arrange(() => mock.Echo(Arg.AnyInt)).DoInstead((int arg1) => expectedResult = arg1);
Replace an implementation.
Mock.Arrange(() => mock.Count()).Returns(42); Arrange a method to return a predefined value.
Mock.Arrange(() => mock.Name).Returns("John Doe"); Arrange a property getter to return a predefined value.
Mock.Arrange(() => foo.Get<string, int>(Arg.AnyInt)).Returns("Alice"); Arrange a generic method to return a predefined value.
Mock.ArrangeSet(() => mock.Name = "Bob"); Arrange a property setter.
Mock.Arrange(() => mock.GetCount()).Returns(7).InSequence();
Mock.Arrange(() => mock.GetCount()).Returns(8).InSequence();
Mock.Arrange(() => mock.GetCount()).Returns(9).InSequence();
Return multiple values in a sequence. Every subsequent call after the third will return the last specified value.
int[] returnValues = new int[3] { 7, 8, 9 };
Mock.Arrange(() => mock.GetCount()).ReturnsMany(returnValues);
Return multiple values specified in an array. If the number of the calls exceeded the array size than the last specified value will be returned.
Mock.Arrange(() => mock.Products.ReturnsCollection(CollectionProducts()); Return of a predefined colleciton.
string expected = "MoveUp";
Mock.Arrange(() => mock.TryProcessRequest(ref expected)).Returns(true);
Arrange a method with ref parameter.
int expected = 3;
Mock.Arrange(() => mock.TryGetCount(out expected).Returns(true);
Arrange a method with out parameter.
Mock.Arrange(() => mock.GetCountAsync()).TaskResult(42); Arrange async method.
Mock.Arrange(() => mock.Echo(-1)).Throws(); Throws exception of type ArgumentException.
Mock.Arrange(() => mock.Echo(-1)).ThrowsAsync(); Throws exception of type ArgumentException for an async method.

Arrangements with argument matchers

The argument matchers are particularly useful when you want to match a broader range of arguments.

Code Description
Mock.Arrange(() => mock.Echo(1)); Plan
Mock.Arrange(() => mock.Echo(Arg.AnyInt)); Anything
Mock.Arrange(() => mock.Echo(Arg.IsInRange(1, 5, RangeKind.Inclusive))); In Range
Mock.Arrange(() => mock.Echo(Arg.Matches(x => x < 10))); Predicate
Mock.Arrange(() => mock.ProcessRequest(ref Arg.Ref("MoveUp").Value)); Ref parameter
Mock.Arrange(() => mock.TryGetCount(out Arg.Ref(9).Value)); Out parameter
Mock.Arrange(() => mock.Echo(0)).IgnoreArguments(); Ignore all arguments

Occurrence

You could add occurrence verifications along the arrangements. To later execute them use the Mock.Assert(mock) method.

Code Description
Mock.Arrange(() => mock.Do()).OccursOnce(); Once
Mock.Arrange(() => mock.Do()).Occurs(2); Exactly 2 times
Mock.Arrange(() => mock.Do()).MustBeCalled(); At least once
Mock.Arrange(() => mock.Do()).OccursAtLeast(1); At least exact number of times
Mock.Arrange(() => mock.Do()).OccursAtMost(2); At most 2 times
Mock.Arrange(() => mock.Do()).OccursNever(); Never

Assert occurrences

There is another approach to assert that a certain method was executed when you haven't made an arrangement.

Code Description
Mock.Assert(() => mock.Do(), Occurs.Once()); Once
Mock.Assert(() => mock.Do(), Occurs.Exactly(2)); Exactly 2 times
Mock.Assert(() => mock.Do(), Occurs.AtLeastOnce()); At least once
Mock.Assert(() => mock.Do(), Occurs.AtLeast(1)); At least exact number of times
Mock.Assert (() => mock.Do(), Occurs.AtMost(2)); At most 2 times
Mock.Assert(x => x.Do(), Occurs.Never()); Never

Assertion with argument matchers

Code Description
Mock.Assert(() => mock.Name); Property get
Mock.AssertSet(() => mock.Name = "Alice"); Property set
Mock.Assert(() => mock.GetCount(1)); Method call
Mock.Assert(() => mock.Echo(Arg.AnyInt)); Anything
Mock.Assert(() => mock.Echo(Arg.IsInRange(1, 5, RangeKind.Inclusive))); In range
Mock.Assert(() => mock.Echo(Arg.Matches(x => x < 10))); Predicate
Mock.Assert(() => mock.Echo(ref Arg.Ref("MoveUp").Value)); Ref parameter
Mock.Assert(() => mock.TryGetCount(out Arg.Ref(9).Value)); Out parameter

Future mocking

Future mocking allows you to mock members without passing the dependency through a constructor or calling a method.

Code Description
Mock.Arrange(() => foo.ReturnFive()).IgnoreInstance().Returns(7); Arrange future calls to a method from all possible instances to return a predefined value.
Mock.Arrange(() => new Foo()).Return(()=> new Foo(){IsCacheDisabled=true}); Directly arranging the constructor to return an instance created with different contructor overload.

Mocking across threads

Code Description
Mock.Arrange(() => DateTime.Now).Returns(new DateTime(2020, 1, 1)).OnAllThreads(); Mocking DateTime.Now to return a predefined date on all threads.

Mocking extension method

Mocking of an extension method is the same as mocking normal method.

Code Description
Mock.Arrange(() => foo.ExtensionMethod).Returns(42) Arrange an extension method to return a predefined value.

Static mocking

Static mocking allows you to mock static constructors, methods and properties calls, set expectations and verify results.

Code Description
Mock.SetupStatic(typeof(Foo), StaticConstructor.Mocked);
Mock.Arrange(() => Foo.GetStaticValue()).Returns(10);
Arrange a static method to return a predefined value.

Mocking non-public members

Mocking non-public members is useful when you want to isolate calls to non-public members.

Code Description
Mock.NonPublic.Arrange(foo, "PrivateEcho", Arg.Expr.IsAny()).Returns(42); Arrange a private method to return a predefined value.
Mock.NonPublic.Arrange(typeof(Foo), "PrivateStaticProperty").Returns(42); Arrange a private static method to return a predefined value.
In this article