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
I have this simple method that I want to test:

public static void DoIt1(out Guid outValue1)
{
    outValue1 = new Guid("{11111111-1111-1111-1111-111111111111}");
}


And the respective test:

[TestMethod()]
[VerifyMocks]
public void DoIt1Test()
{
    Guid expectedGuidValue1 = Guid.NewGuid();

    using (RecordExpectations recorder = RecorderManager.StartRecording())
    {
        recorder.DefaultBehavior.CheckArguments();

        DoIt1(out expectedGuidValue1);
    }

    Guid actualGuidValue1;
    DoIt1(out actualGuidValue1);

    Assert.AreEqual(expectedGuidValue1, actualGuidValue1, "First Value");
}


I'm getting a very erratic behavior from this. Sometimes actualGuidValue1 equals expectedGuidValue1 and other times actualGuidValue1 just comes out empty (default(Guid)).
asked by paulo.morgado (11k points)

6 Answers

0 votes
Hi Paulo
I couldn't re-create the problem.
Can you please tell me how often it happens?
answered by ohad (35.5k points)
0 votes
I've recoded the test like this:

namespace TestProject
{
    public class TestedClass
    {
        public static void DoIt1(out Guid outValue1)
        {
            outValue1 = new Guid("{11111111-1111-1111-1111-111111111111}");
        }
    }

    [TestClass]
    public class TextFixture
    {
        [TestMethod]
        public void TestMethod1()
        {
            Guid expectedGuidValue1 = Guid.NewGuid();

            using (RecordExpectations recorder = RecorderManager.StartRecording())
            {
                recorder.DefaultBehavior.CheckArguments();

                TestedClass.DoIt1(out expectedGuidValue1);
            }

            Guid actualGuidValue1;
            TestedClass.DoIt1(out actualGuidValue1);

            Assert.AreEqual(expectedGuidValue1, actualGuidValue1, "TestMethod1");
        }

        [TestMethod]
        public void TestMethod2()
        {
            Guid expectedGuidValue1 = Guid.NewGuid();

            using (RecordExpectations recorder = RecorderManager.StartRecording())
            {
                recorder.DefaultBehavior.CheckArguments();

                TestedClass.DoIt1(out expectedGuidValue1);
            }

            Guid actualGuidValue1;
            TestedClass.DoIt1(out actualGuidValue1);

            Assert.AreEqual(expectedGuidValue1, actualGuidValue1, "TestMethod2");
        }
    }
}


And it's the same. It doesn't happen all the time, but happens, often.

By the trace I can't make what's going on.

First I thought it was something related to ClearMocksAttribute and VerifyMocksAttribute, but it's not.
answered by paulo.morgado (11k points)
0 votes
Hi Paulo
Sorry, still can't create the problem.
Can you please send me tracer output?
I'll send you my address offline.
answered by ohad (35.5k points)
0 votes
I still can't force this to happen, but I know when it's going to happen.

The tested class is this:

public class TestedClass
{
    public bool TestedMethod(string token, out Guid firstKey, out Guid secondKey)
    {
        firstKey = new Guid("{11111111-1111-1111-1111-111111111111}");
        secondKey = new Guid("{22222222-2222-2222-2222-222222222222}");
        return true;
    }
}


This is the test that, sometimes, fails:

[TestMethod]
[VerifyMocks]
public void TestMethod1()
{
    Guid firstKey;
    Guid secondKey;
    Guid expectedFirstKey = new Guid("{33333333-3333-3333-3333-333333333333}");
    Guid expectedSecondKey = new Guid("{44444444-4444-4444-4444-444444444444}");
    string token = "token";
    bool expected = true;
    bool actual;

    TestedClass target = RecorderManager.CreateMockedObject<TestedClass>(Constructor.Mocked, StrictFlags.AllMethods);

    using (RecordExpectations recorder = RecorderManager.StartRecording())
    {
        recorder.DefaultBehavior.CheckArguments();

        recorder.ExpectAndReturn(target.TestedMethod(token, out expectedFirstKey, out expectedSecondKey), expected);
    }

    actual = target.TestedMethod(token, out firstKey, out secondKey);

    Assert.AreEqual(expected, actual);
    Assert.AreEqual(expectedFirstKey, firstKey);
    Assert.AreEqual(expectedSecondKey, secondKey);
}


So I decided to used a CustomChecker to make sure the output parameters where the ones I wanted and found out that the mocking always fails when the value of the ArgumentValue is null.

Here is the new test:

private static bool CustomChecker(ParameterCheckerEventArgs data)
{
    if (data.ArgumentValue == null)
    {
        Assert.Inconclusive("TypeMock failure.");
    }

    return true;
}

[TestMethod]
[VerifyMocks]
public void TestMethod2()
{
    Guid firstKey;
    Guid secondKey;
    Guid expectedFirstKey = new Guid("{33333333-3333-3333-3333-333333333333}");
    Guid expectedSecondKey = new Guid("{44444444-4444-4444-4444-444444444444}");
    string token = "token";
    bool expected = true;
    bool actual;

    TestedClass target = RecorderManager.CreateMockedObject<TestedClass>(Constructor.Mocked, StrictFlags.AllMethods);

    using (RecordExpectations recorder = RecorderManager.StartRecording())
    {
        recorder.DefaultBehavior.CheckArguments();

        recorder.ExpectAndReturn(target.TestedMethod(token, out expectedFirstKey, out expectedSecondKey), expected)
            .WhenArgumentsMatch(token,
                Check.CustomChecker(CustomChecker, expectedFirstKey),
                Check.CustomChecker(CustomChecker, expectedSecondKey));
    }

    actual = target.TestedMethod(token, out firstKey, out secondKey);

    Assert.AreEqual(expected, actual);
    Assert.AreEqual(expectedFirstKey, firstKey);
    Assert.AreEqual(expectedSecondKey, secondKey);
}
answered by paulo.morgado (11k points)
0 votes
Hi Paulo
This looks like a bug.
We succeed in creating the behavior you describe with the new test.
Thanks for reporting it.
We will send you the fix once it's ready.
answered by ohad (35.5k points)
0 votes
THANK YOU!

I thought I was going crazy. :P
answered by paulo.morgado (11k points)
...