This is a migrated thread and some comments may be shown as answers.

Assert Occurrence of Static Mock

7 Answers 453 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 07 Oct 2013, 01:22 PM
I can't figure out how to Assert the number of occurrences on a static Mock.  The below doesn't work.
Mock.SetupStatic(typeof(DataAccess), StaticConstructor.Mocked);
Mock.Arrange(() => DataAccess.IsRequestAuthenticate(null))
    .IgnoreArguments().IgnoreInstance()
    .Returns(true)
    .OccursOnce();
 
Mock.Assert(() => DataAccess.IsRequestAuthenticate(null));

7 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 08 Oct 2013, 08:58 AM
Hi Robert,

Thank you for contacting our support.

To answer your question, I'll use the following system under test for example:
static class DataAccess
{
    public static bool IsRequestAuthenticate(string str)
    {
        throw new NotImplementedException();
    }
}

To assert occurrence against a certain static arrangement, you can choose one of the following approaches:

In the first example, we will arrange that a specific static method should occur once and in the same time return true, no matter its arguments. Then we will act by calling the IsRequestAuthenticate method with "test" as an argument. To assert the occurrence, we need to use Mock.Assert. However, as we want to check for all method calls, we will  also need ignore the method arguments in the assert:
[TestMethod]
public void TestMethod1()
{
    // Arrange
    Mock.Arrange(() => DataAccess.IsRequestAuthenticate(null))
        .IgnoreArguments()
        .Returns(true)
        .OccursOnce();
 
    // Act
    var actual = DataAccess.IsRequestAuthenticate("test");
 
    // Assert
    Mock.Assert(() => DataAccess.IsRequestAuthenticate(null), Args.Ignore());
}

If the above Args.Ignore() is not specified, the test will fail. This is so, because the assert will expect that the IsRequestAuthenticate method is called once with null as an argument. This is shown in the next example in which you assert only against an argument specific call:
[TestMethod]
public void TestMethod2()
{
    // Arrange
    Mock.Arrange(() => DataAccess.IsRequestAuthenticate(null))
        .IgnoreArguments()
        .Returns(true)
        .OccursOnce();
 
    // Act
    var actual = DataAccess.IsRequestAuthenticate(null);
 
    // Assert
    Mock.Assert(() => DataAccess.IsRequestAuthenticate(null));
}

Further, I removed the IgnoreInstance() from you arrangement, as it is not needed. This is so, because you are arranging against static member.

I hope the above helps. Let me know if there is more I can help you with.

Regards,
Kaloyan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
Robert
Top achievements
Rank 1
answered on 09 Oct 2013, 06:03 PM
That doesn't seem to work for the following...
Mock.Arrange(() => DataAccess.UpdateUserStatus(null))
    .IgnoreArguments().IgnoreInstance()
    .Returns(1)
    .OccursNever();
 
Mock.Assert(() => DataAccess.UpdateUserStatus(null), Args.Ignore());

0
Robert
Top achievements
Rank 1
answered on 09 Oct 2013, 06:11 PM
I guess because I should be doing this...
Mock.Assert(() => DataAccess.UpdateUserStatus(null), Args.Ignore(), Occurs.Never());
0
Robert
Top achievements
Rank 1
answered on 09 Oct 2013, 07:01 PM
However this doesn't work...
// From the null forward (null, 0, 0, 0) are optional parameters
Mock.Arrange(() => DataAccess.LogEventToDatabase(0, 0, null, 0, 0, 0))     .IgnoreArguments()
    .Returns(1)
    .OccursAtLeast(1);
 
Mock.Assert(() => DataAccess.LogEventToDatabase(0, 0, null, 0, 0, 0), Args.Ignore(), Occurs.AtLeastOnce());
0
Stefan
Telerik team
answered on 10 Oct 2013, 07:08 AM
Hello Robert,

I tested your snippet, and it failed with "Occurrence expectation failed. Expected at least 1 call. Calls so far: 0", as expected. What do you mean by saying that it doesn't work?

Regards,
Stefan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
Robert
Top achievements
Rank 1
answered on 10 Oct 2013, 01:09 PM
It is working.  I was making a mistake.  thanks.
0
Kaloyan
Telerik team
answered on 11 Oct 2013, 06:24 AM
Hello Robert,

To assist you further, I would suggest checking this article from our online help documentation. There, you will be able see some good examples in the AAA (Arrange/Act/Assert) pattern.

Also, here is explained how to mock static members with JustMock.

I hope you find this helpful. Let me know if there is anything else I can assist you with.

Regards,
Kaloyan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
James
Top achievements
Rank 1
Iron
commented on 13 Jun 2025, 06:01 PM | edited

 

Is there not a way to do this for all static methods all at once? With instance members it is enough to assert the mock object like below...

Mock.Assert(mockObject)

However the above doesn't work with static methods and properties... the occurrence expectations on static members will throw an exception if that member is actually called (and the expectation fails), but if you setup mocks for static members that do not get called those occurrence expectations are never evaluated. This can lead to having several mocks setup that are unnecessary. Is the only way to prevent this to have a Mock.Assert() call for each static member at the end? I.E.

Mock.Arrange(() => StaticClass.StaticMethod1()).Returns(null).Occurs(1);
Mock.Arrange(() => StaticClass.StaticMethod2()).Returns(null).Occurs(2);
Mock.Arrange(() => StaticClass.StaticMethod3()).Returns(null).Occurs(3);
Mock.Arrange(() => StaticClass.StaticMethod4()).Returns(null).Occurs(4);

...

StaticClass.StaticMethod1();
StaticClass.StaticMethod2();
StaticClass.StaticMethod2();
StaticClass.StaticMethod2();
StaticClass.StaticMethod3();
StaticClass.StaticMethod3();

...

Mock.Assert(StaticClass.StaticMethod1(), Args.Ignore(), Occurs.Exactly(1));
Mock.Assert(StaticClass.StaticMethod2(), Args.Ignore(), Occurs.Exactly(2)); => Will fail because it was called 3 times
Mock.Assert(StaticClass.StaticMethod3(), Args.Ignore(), Occurs.Exactly(3)); => Will fail because it was called 2 times.
// StaticClass.StaticMethod4() was never called, so the occurrence expectation that it was called 4 times is never evaluated.
Why can we not do something like this?
Mock.AssertStatic(StaticClass);
James
Top achievements
Rank 1
Iron
commented on 13 Jun 2025, 06:11 PM

Actually, it appears you can do the above.
Mock.Assert(typeof(StaticClass);
Ivo
Telerik team
commented on 16 Jun 2025, 01:43 PM

Hello James, that is the right way to assert static method mocks.
Tags
General Discussions
Asked by
Robert
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Robert
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or