Temporary Static Mocking

2 posts, 0 answers
  1. Micah
    Micah avatar
    6 posts
    Member since:
    Sep 2013

    Posted 01 Nov 2013 Link to this post

    I have some legacy code I am trying to migrate to Just Mock.  There are some static objects that many of our older tests reference.  I am slowly starting to re-write these tests with Static Mocking.

    How can I have some of my tests use the mocked static while others continue using the old un-mocked static?  Ideally, I would like some code I can put either at the end of my test, in a using statement, or in my test/fixture teardown that would revert the static object to its original state and cause the next call to it to execute the static constructor/initialization.

    Something like this:

    class MyClass
        public static int SomeStatic { getprivate set;}
        static MyClass()
            SomeStatic = 15;
    public void foo()
        using (Mock.SetupStatic(typeof(MyClass), Behavior.StrictStaticConstructor.Mocked));
            Mock.Arrange(() => MyClass.SomeStatic).Returns(5);
            Assert.AreEqual(5, MyClass.SomeStatic);
        Assert.AreEqual(15, MyClass.SomeStatic);
  2. Kaloyan
    Kaloyan avatar
    871 posts

    Posted 06 Nov 2013 Link to this post

    Hi Micah,

    Thank you for bringing this topic.

    Unfortunately, such functionality is not achievable with JustMock. This means, once the static constructor is mocked it will stay mocked for that current AppDomain.

    However, I like to suggest a couple of possible workarounds for this unwanted behavior:
    1. Add the static constructor logic into a helper method.Talking about legacy code, this approach should be most appropriate. What I mean is something like this:
      public class MyClass
          public static int SomeStatic { get; private set; }
          static MyClass()
              SomeStatic = 15;
          public static void IntializeCtor()
              SomeStatic = 15;
      Then you will be able to execute the constructor logic in any test that needs the original behavior of MyClass. Take the following example:
      public void foo1()
          Mock.SetupStatic(typeof(MyClass), Behavior.Strict, StaticConstructor.Mocked);
          Mock.Arrange(() => MyClass.SomeStatic).Returns(5);
          Assert.AreEqual(5, MyClass.SomeStatic);
      public void foo2()
          Assert.AreEqual(15, MyClass.SomeStatic);
      Both of the tests will pass no matter their execution order. Of course, to make this cleaner, you can call the IntializeCtor method inside test/fixture initialize or tear-down.

    2. Separate the tests (the ones using StaticConstructor.Mocked and the ones following the original logic) to different assemblies. Doing this, you will be able to execute them in different AppDomains. I achieved this behavior by creating two different test projects. One for the tests that needed the static constructor mocked and another test project for the rest. Then, running all tests via the Visual Studio 2012 Test Runner (vsexecutionengine) resulted with success, again no matter the test execution order. This is also achievable with the Nunit test runner, as explained here.

    I hope the proposed workarounds helps. Please, let me know if you need further assistance on this or anything else concerning our products.

    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
Back to Top