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
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
asked by chartek (600 points)

3 Answers

0 votes
It seems we have a bug in NonPublic generics handling.

Thank you for the issue report and the simple repro.


We'll investigate this issue.
answered by dhelper (11.9k points)
0 votes
This issue still exists in version 5.3. Any ETA on when this fix will be in?
answered by csustek (4k points)
0 votes
Hi,

This issue was reported in version 5.3, and is fixed on our development build. It will of course be fixed in the upcoming Isolator release.

I will send you an email with a patched version containing this fix. Let's make sure offline this indeed resolves the issue.

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
...