chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Given (see code):

- using MSTest
- in the test setup (marked with [TestInitialize]), a class is faked using the AAA API
- the faked class contains a property of another type, which has a static member

Bug: the static members of the latter type are not being initialized

using Microsoft.VisualStudio.TestTools.UnitTesting;
using TypeMock.ArrangeActAssert;

  [TestClass]
  public class DummyTest
  {
    [TestInitialize]
    public void setup()
    {
      Isolate.Fake.Instance<Dummy>();
    }

    [TestMethod]
    public void StaticMembersOfUsedInDummy_WhenCalled_ShouldBeInitialized()
    {
      Assert.IsNotNull(UsedInDummy.Test);
    }

  }

  class Dummy 
  {
    public UsedInDummy SomeProperty { get; set; } 
  }

  class UsedInDummy
  {
    public static readonly string Test = "test";
  }

asked by jeroenh (680 points)

7 Answers

0 votes
Hi,

UsedInDummy.Test is null because it is static property while you told the Isolator to fake instance methods and properties.

Change the setup method to:
[TestInitialize]
public void setup()
{
    Isolate.Fake.StaticMethods<Dummy>();
}


This will make the test pass 8)
answered by ohad (35.4k points)
0 votes
The code I posted was a stripped down example of a typical AAA-style test.

Please note that I'm NOT faking UsedInDummy itself, I'm faking Dummy. I needed a fake 'Dummy' (NOT a UsedInDummy!!), so using the AAA syntax I set it up like:

Dummy sut = Isolate.Fake.Instance<Dummy>();


It is kind of unexpected that, given Dummy has a property of type UsedInDummy, the static properties of the latter would not be initialized... This caused the test to fail because some code down the path used the static member from UsedInDummy.

I still consider this to be a (severe!) bug...
answered by jeroenh (680 points)
0 votes
Hi,
Sorry, this is my mistake.
I checked it out and you are right we have a bug.
The bug is that when you use recursive fakes (the default of Isolate.Fake.Instance) the Isolator fakes the return value of all methods and properties automatically. The Isolator should not fake static constructors but apparently it does :oops:

A possible workaround is to use other options other than Memberes.ReturnRecursiveFakes like Members.ReturnNulls.
[TestInitialize]
public void setup()
{
    Isolate.Fake.Instance<Dummy>(Members.ReturnNulls);
}


I'll let you know as soon as we will fix the bug.
answered by ohad (35.4k points)
0 votes
Once you know about the bug, working around the problem is easy. E.g. another workaround is to make sure the static constructor has run before the instance is faked.


[TestInitialize] 
public void setup() 
{ 
    // make sure static constructor has run
    var t = UsedInDummy.Test;

    Isolate.Fake.Instance<Dummy>(); 
} 


But unfortunately that does not bring you back the time you spent initially, lonesome and puzzled :?, trying to solve this weird test failure...
answered by jeroenh (680 points)
0 votes
Hi,

Sorry for the lost time :oops:
I hope we'll fix the bug soon. I'll let you know once we'll have a patch.
answered by ohad (35.4k points)
0 votes
Hi,

Has a fix been released?

Thanks,

Jason

Hi,

Sorry for the lost time :oops:
I hope we'll fix the bug soon. I'll let you know once we'll have a patch.
answered by JasonW (2.3k points)
0 votes
Hi Jason,
Sorry, not yet.
Have you run into similar problem?
answered by ohad (35.4k points)
...