RajOpteamix / moq

Automatically exported from code.google.com/p/moq
Other
0 stars 0 forks source link

Moq of class with override of Equals() crashes testing framework #362

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
[TestClass]
public class StackOverflow
{
    [TestMethod]
    public void CreateStackOverflow()
    {
        var mockMyType1 = new Mock<MyType>();
        mockMyType1.Setup(m => m.Equals(mockMyType1.Object)).Returns(true);

        var mockMyType2 = new Mock<MyType>();

        // Real test is for a filtering routine and the Assert is using
        // Contains(), but it uses Equals() internally so it has the same problem
        Assert.IsTrue(mockMyType1.Object.Equals(mockMyType1.Object)); // returns true
        Assert.IsFalse(mockMyType1.Object.Equals(mockMyType2.Object)); // explodes
    }
}

public class MyType
{
    public virtual bool IsActive { get; set; }
    public override bool Equals(object obj)
    {
        // Not the real implementation but irrelevant to this issue
        return object.ReferenceEquals(this, obj);
    }
}

What is the expected output? What do you see instead?
I would expect the test to pass.  Instead, QTAgent32.exe crashes.  When the 
test is run in Debug mode, I get "An unhandled exception of type 
'System.StackOverflowException' occurred in Unknown Module."

What version of the product are you using? On what operating system?
Moq - 4.0.10827.0
Visual Studio 2010 Ultimate - 10.0.40219.1 SP1 Rel
Windows 7 Enterprise SP 1 (32 bit)

Please provide any additional information below.
In the actual code, both Equals() and GetHashCode() are overridden in a base 
class.  The following work fine:
 - If MyType does not override Equals(), the test passes
 - If I don't Setup(Equals()) and use CallBase=true instead, it passes

Original issue reported on code.google.com by RPMcNa...@gmail.com on 3 Apr 2013 at 12:54