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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
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);
        }
    }
asked by (3.9k points)

8 Answers

0 votes
It looks like you have found a bug here.

I'm not sure what the exact problem is, but it relates to wrong handling of the Regex static constructor.

In any case we shall investigate this and get back to you on this.
answered by (13.2k points)
0 votes
From this thread https://www.typemock.com/community/viewtopic.php?t=775 it seems like it is not a bug.
Static constructors are mocked by default.
I don't like this change since it can result in some strange exceptions like the one in this thread.
It seems like the documentaion needs a update.
Is it possible to execute the static constructor using natural mocks?
Could it be an idea to make the execution of static constructors optional?
br
Arnulf
answered by (3.9k points)
0 votes
Hi,

On the surface it seems that you are right. However, after going deeper the cause in your case is different.

Based on what I've seen so far for some reason the static ctor of the RegEx class is not activated since the framework thinks it should be mocked when clearly it should not be (since the user didn't specify it).

In the other thread the default behavior of the API itself has changed.

:!: The API change was made in order to make default behavior consistent with all API's.
answered by (13.2k points)
0 votes
Hi Lior.
Can you confirm that this is a bug.
Should we the post to the Bugs Forum.
Arnulf
answered by (3.9k points)
0 votes
Hi,

Yes. This is a bug.
I don't think we should post this into the bug forum. its started here let keep it here.
answered by (13.2k points)
0 votes
Everybody agrees that interacting test should be avoided at all costs.
But the last changes to Typemock introduces just that in my tests.
Here is the case, in my class I have private static field:
private static TraceSwitch traceSwitch = new TraceSwitch("ClassWithStaticFieldSwitch", "Enable trace for the ClassWithStaticField");


When I write my tests the traceSwitch has a value valid value and my tests are all happy.

Then another developer starts writing tests on a another class that is using my class.
He mocks a function in my class.
What happens is that my tests starts to fail because the traceSwitch field has been mocked.

I don't like this behavior.
answered by (3.9k points)
0 votes
Hi,

Lets make some order to this all mess.
I'm starting to feel that we are going the wrong direction here.

First lets understand that there is a BUG here that needs to be solved.
(and BTW I don't think that the new field mocking is the cause of it)

What you see in your scenario is the static constructor being mocked (and not really activated) in the other developer test and not reactivated later as the isolator framework should have done.

This can easily be verified by disabling the field mocking.
My guess is tests will still fail but with NullReferenceException instead.

So, to make story short, Lets see what will happen after we introduce a fix for the original bug. I really think that it will make most of the problems go away.
[/list]
answered by (13.2k points)
0 votes
Hi Everyone,

We've got the problem resolved on v4.3. You can download it from:https://www.typemock.com/Downloads.php
answered by (14.5k points)
...