Assert Occurrence of Static Mock

8 posts, 0 answers
  1. Robert
    Robert avatar
    20 posts
    Member since:
    Jul 2013

    Posted 07 Oct 2013 Link to this post

    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));
  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 08 Oct 2013 Link to this post

    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.
  3. DevCraft R3 2016 release webinar banner
  4. Robert
    Robert avatar
    20 posts
    Member since:
    Jul 2013

    Posted 09 Oct 2013 Link to this post

    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());

  5. Robert
    Robert avatar
    20 posts
    Member since:
    Jul 2013

    Posted 09 Oct 2013 Link to this post

    I guess because I should be doing this...
    Mock.Assert(() => DataAccess.UpdateUserStatus(null), Args.Ignore(), Occurs.Never());
  6. Robert
    Robert avatar
    20 posts
    Member since:
    Jul 2013

    Posted 09 Oct 2013 Link to this post

    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());
  7. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 10 Oct 2013 Link to this post

    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.
  8. Robert
    Robert avatar
    20 posts
    Member since:
    Jul 2013

    Posted 10 Oct 2013 Link to this post

    It is working.  I was making a mistake.  thanks.
  9. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 11 Oct 2013 Link to this post

    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.
Back to Top
DevCraft R3 2016 release webinar banner