Mocking Non-public Members and Types just passes sometimes

2 posts, 1 answers
  1. Lukas
    Lukas avatar
    20 posts
    Member since:
    Jul 2011

    Posted 30 May 2012 Link to this post

    I have a couple of calls like this in my class to be testet, here a snippet:
    public void AddKeys(bool resultIsValid)
        if (!resultIsValid)

    I'm absolutely sure that my code works. I think the problem might be that I call a method on a static member in the LogMyException method. Anyhow, when I run the tests one by one, it works everytime. If I run them all at once they sometimes pass, but sometimes they don't. The LogMyException method is a private method and I test if LogMyException gets called as follows:

    public void Test()
        var called = false;
        // Initialize test class
        Mock.NonPublic.Arrange(confStringManager, "LogMyException"ArgExpr.IsAny<string>()).DoInstead(() => called = true);
        // Do something which makes the class call LogMyException
        Assert.True(called); }

  2. Answer
    Ricky avatar
    467 posts

    Posted 01 Jun 2012 Link to this post

    Hi Lukas,

    Thanks again for contacting us.

    However, it could be that LogMyException is called in some other test method(s) that is preventing JM to intercept it in later tests.

    I would recommend you to take a look into the following post that should solve your problem:

    Kind Regards
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top