Upgrading to 4.2.2 made some of our tests fail.
The documentation states that:
Static constructors will always be called. To mock a static constructor, call recorder.MockStaticConstructors(); this will mock all static constructors that were called while recording
This does not seem to be the case. Ref sample below.
The static constructor of RegEx is not called and the tests using natural mocks fail.
[TestClass]
public class TypeMockMocksRegExTest
{
[TestMethod, VerifyMocks]
public void Replace_Reflective_Success()
{
ClassWithStaticConstructor classWithStaticConstructor = (ClassWithStaticConstructor)RecorderManager.CreateMockedObject(typeof(ClassWithStaticConstructor), Constructor.StaticNotMocked);
Assert.AreEqual("MMMM1231", classWithStaticConstructor.Replace(@"!#!#1231", "M"));
}
[TestMethod, VerifyMocks]
public void TypeMockMocksRegExTest_Natural_Failes()
{
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
ClassWithStaticConstructor mockedClassWithStaticConstructor = new ClassWithStaticConstructor();
}
ClassWithStaticConstructor classWithStaticConstructor = new ClassWithStaticConstructor();
Assert.AreEqual("MMMM1231", classWithStaticConstructor.Replace(@"!#!#1231", "M"));
}
[TestMethod, VerifyMocks]
public void TypeMockMocksRegExTest_NaturalCallStaticConstructors_Failes()
{
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
ClassWithStaticConstructor mockedClassWithStaticConstructor = new ClassWithStaticConstructor();
recorder.CallStaticConstructors();
}
ClassWithStaticConstructor classWithStaticConstructor = new ClassWithStaticConstructor();
Assert.AreEqual("MMMM1231", classWithStaticConstructor.Replace(@"!#!#1231", "M"));
}
}
public class ClassWithStaticConstructor
{
static Regex precodeChecker = new Regex("(?<illegalchar>[^a-zA-Z0-9_-])", RegexOptions.Compiled);
public string Replace(string input, string replacement)
{
return precodeChecker.Replace(input, replacement);
}
}