devlooped / moq

The most popular and friendly mocking framework for .NET
Other
5.88k stars 801 forks source link

Stack overflow when setting up a property on a mock that references another instance of the same type #161

Closed ArildF closed 7 years ago

ArildF commented 9 years ago

The following code gives a StackOverflowException in Moq 4.2.1502.911, but not in 4.1.1309.1617:

public interface IFoo
{
    IBar Bar { get; }
}

public interface IBar
{
    IBar SomeOtherBar { get; }
    string ATC { get; }
}

[TestMethod]
public void SomeTest()
{
    var factory = new MockRepository(MockBehavior.Strict){DefaultValue = DefaultValue.Mock};
    var foo = factory.Create<IFoo>();
    foo.SetupGet(r => r.Bar.ATC).Returns("asjkhsd"); // boom
}

This appears to be a similar problem to https://code.google.com/p/moq/issues/detail?id=332 , although in this case the result is a stack overflow and not an infinite loop as it appears in that issue.

Using DefaultValue.Empty avoids the problem; however, this may not be a practical solution in our case. The value of MockBehavior does not matter.

stakx commented 7 years ago

This appears to have been fixed. The error can no longer be reproduced in Moq 4.7.25.