You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After upgrading to Moq 4.2.1502.911 we are seeing what we believe is a bug in Moq when attempting to cast a mocked type to an Interface. We've produced a test case below along with a few unit tests.
namespace MoqBug
{
using System.Collections.Generic;
using System.Linq;
using Moq;
using NUnit.Framework;
[TestFixture]
public class FunWithTypes
{
[Test]
public void MoqErrors()
{
Mock<ExtendingTypeBase> fake = new Mock<ExtendingTypeBase>(42);
ExtendingTypeBase realFake = fake.Object;
// Make sure we're telling the truth that the value is mocked prior to our frobbing
Assert.That(realFake.ExtendedValue, Is.EqualTo(42));
Frobber frobber = new Frobber(realFake);
Assert.That(frobber.WasExtendedType, Is.True);
// BUGBUG: Seems to have been set back to the default for this type (Moq type is by default LOOSE)
Assert.That(frobber.ExtendedTypeValue, Is.EqualTo(42), "BUGBUG: Moq Lost the value and set back to default.");
}
[Test]
public void CSharpIsCoolWithIt()
{
ExtendingTypeBase real = new ExtendedConcreteType(42);
// Make sure we're telling the truth that the value is mocked prior to our frobbing
Assert.That(real.ExtendedValue, Is.EqualTo(42));
Frobber frobber = new Frobber(real);
Assert.That(frobber.WasExtendedType, Is.True);
Assert.That(frobber.ExtendedTypeValue, Is.EqualTo(42));
}
[Test]
public void MoqErrorsTwo()
{
var rootMock = new Mock<ExtendingTypeBase>(42);
ExtendingTypeBase realObject = rootMock.Object;
IExtendedType realObjectInterfaceType = rootMock.Object;
Assert.That(realObject.ExtendedValue, Is.EqualTo(42));
// BUGBUG: Seems to have been set back to the default for this type (Moq type is by default LOOSE)
Assert.That(realObjectInterfaceType.ExtendedValue, Is.EqualTo(42));
}
[Test]
public void MoqErrorsThree()
{
var rootMock = new Mock<ExtendingTypeBase>(42);
ExtendingTypeBase realObject = rootMock.Object;
IExtendedType realObjectInterfaceType = rootMock.As<IExtendedType>().Object;
Assert.That(realObject.ExtendedValue, Is.EqualTo(42));
// BUGBUG: Seems to have been set back to the default for this type (Moq type is by default LOOSE)
Assert.That(realObjectInterfaceType.ExtendedValue, Is.EqualTo(42));
}
}
public class Frobber
{
List<ISharedType> internalStore;
public Frobber(params ISharedType[] inputs)
{
// Save the Internal Store
this.internalStore = new List<ISharedType>(inputs);
}
public bool WasExtendedType
{
get
{
return this.internalStore.First() is IExtendedType;
}
}
public int ExtendedTypeValue
{
get
{
return ((IExtendedType)this.internalStore.First()).ExtendedValue;
}
}
}
public class ExtendedConcreteType : ExtendingTypeBase
{
public ExtendedConcreteType(int extendedValue):
base(extendedValue)
{
}
public override int CommonValue
{
get;
set;
}
}
public interface ISharedType
{
int CommonValue { get; set; }
}
public interface IExtendedType
{
int ExtendedValue { get; set; }
}
public abstract class ExtendingTypeBase : ISharedType, IExtendedType
{
public ExtendingTypeBase(int extendedValue)
{
this.ExtendedValue = extendedValue;
}
public abstract int CommonValue { get; set; }
public int ExtendedValue { get; set; }
}
}
Looking at the issues log I think it might be related to #164 and probably the root cause of #156 along with #162. I did a pull of the latest source and the issue is still present, however nothing was readily apparent in my debugging, but I'll admit I did not dig very deep.
The first scenario posted is more or less a very very stripped down version of something we have in our "production" test code, the remaining test cases are what some of our other developers came up with.
The text was updated successfully, but these errors were encountered:
Wow I don't know why I wasn't notified of this fix back in 2017 but for some reason thought about coming back to this bug. You are awesome @stakx Doing some amazing work!
After upgrading to Moq 4.2.1502.911 we are seeing what we believe is a bug in Moq when attempting to cast a mocked type to an Interface. We've produced a test case below along with a few unit tests.
Looking at the issues log I think it might be related to #164 and probably the root cause of #156 along with #162. I did a pull of the latest source and the issue is still present, however nothing was readily apparent in my debugging, but I'll admit I did not dig very deep.
The first scenario posted is more or less a very very stripped down version of something we have in our "production" test code, the remaining test cases are what some of our other developers came up with.
The text was updated successfully, but these errors were encountered: