JustMock

The MustBeCalled method is used to assert that a call to a given method or property is made during the execution of a test.

In this article you will find various examples of the MustBeCalled usage, for which we will be using the following class:

Assert All Calls Marked as Assertable

Let's arrange that a call must be made and then assert that. Only calls marked as assertable, i.e. with MustBeCalled, are verified.

Here we ensure that foo.Echo is called with argument 1, however, the call with argument 2 is not verified.

Throwing Exception When MustBeCalled Setup Is Never Invoked

When MustBeCalled setup for some method is never invoked an exception is thrown. In the following example with foo.Execute()).MustBeCalled(); we specify that foo.Execute is required to be called but this is never done.

Important

In the next example is used NUnit Testing Framework.

As a result, when verifying the foo object, MockAssertion exception is thrown as foo.Execute is never actually called.

Using MustBeCalled For Property Set

Let`s assume we have the following interface:

You can use MustBeCalled when arranging property set. Here is an example:

We use DoNothing to ignore the actual implementation when foo.Value is set to 1 and specify that it must be set to exactly 1 in our test. Before acting with foo.Value = 1; an exception of type MockAssertion would be thrown when asserting foo. After acting we verify that foo.Value was previously set to 1.

Using MustBeCalled When Ignoring Arguments

You can use MustBeCalled in conjunction with IgnoreArguments. Here is an example:

We use IgnoreArguments() to ignore the arguments passed to foo.Execute method and specify that it must be called. Our acting is by foo.Execute(10);. Finally, we verify that the method is actually called with some or other argument.

See Also

Other Resources

[b9461116-b200-4739-aff1-af8458c7095e]