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
I tried to mix reflective and natural mocks and run into this situation.

Tested class:
public class MethodContainer
{
    public static NameValueCollection CalledMethod(IDictionary<string, string> stuff)
    {
        return null;
    }

    public void CallingMethod()
    {
        Dictionary<string, string> stuff = new Dictionary<string, string>();
        CalledMethod(stuff);
        if (stuff.Count <= 0)
        {
            throw new InvalidOperationException();
        }
    }
}


Test:
[TestClass]
[ClearMocks]
public class UnitTest2
{

    [TestMethod]
    [VerifyMocks]
    public void TestMethod2()
    {
        MockObject<MethodContainer> containerMockObject = MockManager.MockObject<MethodContainer>();
        containerMockObject.Strict = true;
        containerMockObject.StrictStatic = true;
        containerMockObject.CallStatic.MethodSettings("CalledMethod").MockMethodCalled += delegate(object sender, MockMethodCallEventArgs e)
            {
                IDictionary<string, string> stuff = e.SentArguments[0] as IDictionary<string, string>;
                stuff.Add("key1", "value1");
            };
        
        NameValueCollection output = new NameValueCollection();
        MethodContainer container = containerMockObject.Object;

        using (RecordExpectations recorder = RecorderManager.StartRecording())
        {
            container.CallingMethod();
            recorder.CallOriginal();

            recorder.ExpectAndReturn(MethodContainer.CalledMethod(null), output).IgnoreArguments();
        }

        container.CallingMethod();
    }
}
asked by paulo.morgado (11k points)

2 Answers

0 votes
Hi Paulo,

Did you forgot by any chance to complete the post?

what is the situation you ran into?

(and sorry for the delayed response)
answered by lior (13.2k points)
0 votes
Looks like I did forget to complete the post.

The problem is that the MockMethodCalled is being fired twice for static methods.

If I change CalledMethod to be an instance method, it only fires once.
answered by paulo.morgado (11k points)
...