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

Temporary Static Mocking

1 Answer 74 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Micah
Top achievements
Rank 1
Micah asked on 02 Nov 2013, 04:36 AM
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;
    }
}
 
[Test]
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);
}

1 Answer, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 06 Nov 2013, 01:05 PM
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:
    [TestMethod]
    public void foo1()
    {
        Mock.SetupStatic(typeof(MyClass), Behavior.Strict, StaticConstructor.Mocked);
     
        Mock.Arrange(() => MyClass.SomeStatic).Returns(5);
        Assert.AreEqual(5, MyClass.SomeStatic);
    }
     
    [TestMethod]
    public void foo2()
    {
        MyClass.IntializeCtor();
     
        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.

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.
Tags
General Discussions
Asked by
Micah
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Share this question
or