Unit Testing Using Fakes With Entity Metadata

When I was writing the Unit test I got stuck at one point where I saw the code which will create 1:N relationships. But the problem was how to achieve that code in the unit test method? I searched everywhere but did not get a proper solution.
 
So in this blog, I am going to explain how to write a method for "EntityMetadata: OneToManyRelationshipMetadata".
 
SDK Reference
  1. using Microsoft.Xrm.Sdk.Metadata;  
Plugin code
  1. RetrieveEntityRequest request = new RetrieveEntityRequest()  
  2. {      
  3.     // Summary: EntityFilters is a ENUM where Relationships value is 8  
  4.     // Use this to retrieve entity information plus entity relationships for the entity.  
  5.     // Value = 8.  
  6.     EntityFilters = EntityFilters.Relationships,  
  7.     LogicalName = "account",  
  8.     RetrieveAsIfPublished = false,  
  9. };  
  10.   
  11. RetrieveEntityResponse response = (RetrieveEntityResponse)service.Execute(request);  
  12. if (response.EntityMetadata != null)  
  13. {  
  14.     foreach (var relationships in response.EntityMetadata.OneToManyRelationships)  
  15.     {  
  16.         if (relationships.IsCustomizable.Value)  
  17.         {  
  18.             var referencingEntity = relationship.ReferencingEntity;  
  19.             var referencingAttribute = relationship.ReferencingAttribute;  
  20.             /*Your code to implement your logic*/  
  21.             ////  
  22.             ////  
  23.         }  
  24.     }  
  25. }  
Based on the above plugin code we will write the unit test method,
  1. [TestMethod]  
  2. public void EntityMetadataMethod()  
  3. {  
  4.     OrganizationService.ExecuteOrganizationRequest = (request) =>  
  5.     {  
  6.       // Summary:  
  7.       //     Defines a managed property that stores a Boolean value.  
  8.       //     For the Web API the corresponding type is BooleanManagedProperty ComplexType.  
  9.       BooleanManagedProperty property = new BooleanManagedProperty()  
  10.       {  
  11.           Value = true  
  12.       };  
  13.   
  14.       // Summary:  
  15.       //     Contains the metadata for a one-to-many entity relationship.  
  16.       OneToManyRelationshipMetadata[] OneToManyRelationships = new OneToManyRelationshipMetadata[]  
  17.       {  
  18.          new OneToManyRelationshipMetadata()  
  19.          {  
  20.            ReferencingEntity = "account",  
  21.            ReferencingAttribute = "primarycontactid",  
  22.            IsCustomizable = property,  
  23.            SchemaName = "new_account_contact"  
  24.          },  
  25.          new OneToManyRelationshipMetadata()  
  26.          {  
  27.            ReferencingEntity = "contact",  
  28.            ReferencingAttribute = "parentaccountid",  
  29.            IsCustomizable = property,  
  30.            SchemaName = "new_contact_account"  
  31.          }  
  32.      };  
  33.   
  34.      // Summary:  
  35.      //     Contains the metadata for an entity.  
  36.      EntityMetadata metadata = new EntityMetadata()  
  37.      {  
  38.         SchemaName = "salesorder"  
  39.      };  
  40.   
  41.      metadata.GetType().GetProperty("OneToManyRelationships").SetValue(metadata, OneToManyRelationships);  
  42.      OrganizationResponse responce = new RetrieveEntityResponse();  
  43.      responce["EntityMetadata"] = metadata;  
  44.      return responce;  
  45.  };  
  46. }  
Using the above unit test method code we can achieve our code coverage. If you want to learn how to write unit test code with different data types and different methods please read my blog on the Unit test - Unit test using fakes.
 
I hope the above solution will help with your code coverage. 
 
Keep learning new things....!!