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
Hi there.

I'm using Isolator 6 and have noticed something odd. I'm testing some CRM 4.0 code which makes use of the CRM BusinessEntity object. So in my unit test, I have this snippet:

FakeBusinessEntity = Isolate.Fake.Instance<BusinessEntity>();


I then use this fake like so:

Isolate.WhenCalled(() => DataImportUtils.GetEntityByIdAndName(null, Guid.Empty, null)).WillReturn(FakeBusinessEntity);


When I debug and step through the code, I notice that GetEntityByIdAndName() is mocked and returns the fake business entity object, but the type of the object is 'Mock0001BusinessEntity' and not 'BusinessEntity'.

Does anyone know why Isolator is renaming the type? The problem is that I have another routine in the code which is now failing with this message:

System.InvalidCastException: Unable to cast object of type 'Mock0001BusinessEntity' to type 'Microsoft.Crm.Sdk.DynamicEntity'.


I'm also faking a IWorkflowContext object and the same problem happens with that, in that the typename starts with 'Mock0001'. This problem is only happening for the CRM related objects.

Cheers.
Jas.
asked by MrClyfar (5.2k points)

2 Answers

0 votes
Hi,

The type you see is a fake type generated at runtime. The class BusinessEntity is abstract, therefor in order to generate an instance Isolator generates a concrete type at runtime.

The reason that the casting fails is that DynamicEntity is a class that inherits BusinessEntity which make the types "brothers" so it's not possible to cast between them. I assume you expect DynamicEntity since a provider usually supply instances of that type.

As a solution you can fake DynamicEntity:
BusinessEntity fake = Isolate.Fake.Instance<DynamicEntity>();


Regards,
Elisha,
Typemock Support
answered by Elisha (12k points)
0 votes
Hi there.

Brilliant, that worked a treat - I'm now faking a <DynamicEntity> instead and the code runs fine now.

Cheers.
Jas.
answered by MrClyfar (5.2k points)
...