Hello -
I am trying to fake an internal (well, this problem exists for protected and private as well) property on a generic class and I believe I stumbled across a bug because when I set a WhenGetCalled on that property, that property always throws a NullReferenceException.
Here is the class I am faking:
public class Class<T>
{
internal T Id { get; set; }
}
Here is my test code:
[Fact]
[Isolated]
public void Can_Fake_Protected_Property()
{
// Arrange
Class<int> obj = Isolate.Fake.Instance<Class<int>>();
Isolate.NonPublic.Property.WhenGetCalled(obj, "Id").WillReturn(5);
// Act
int value = obj.Id;
// Assert
Assert.Equal(0, value);
}
I am using TypeMock 5.3, xUnit 1.1.0.1323, and compiled against .NET 3.5 SP1 in Visual Studio 2008 SP1. No matter if the property is protected, private, or internal, it throws a NullReferenceException. If I change the property to public, it works fine. If I remove the generic <T> from the class (leaving just public class Class {...}), it also works perfectly. I also tried reflective mocks with generics and it resulted in the same NullReferenceException.
Here is the exception detail:
Message: "Object reference not set to an instance of an object."
Stack Trace: " at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
at ViperX.MyProject.Infrastructure.Class`1.get_Id()"
If you need more information than this, please let me know. Thank you for your time and help!
Cheers,
Steven