Unit Test RetrieveResponse in CRM using Microsoft Fakes.

Hi,

Recently we wrote a plugin that was using RetrieveResponse class to get the information of an n – n relationship using Relationship class.

The issue that we faced over here is that the Entity property of RetrieveResponse is read only, we cannot set it. The option was to write the wrapper class over it and use the same wrapper class in the code and the unit test.

/// <summary>
 /// Wrapper class for retrieve response
 /// </summary>
 [DataContract(Namespace = "http://schemas.microsoft.com/xrm/2011/Contracts")]
 public class RetrieveResponseWrapper : OrganizationResponse
 {
 /// <summary>
 /// The _entity
 /// </summary>
 private Entity entity;

/// <summary>
 /// Initializes a new instance of the <see cref="RetrieveResponseWrapper"/> class.
 /// </summary>
 /// <param name="response">The response.</param>
 public RetrieveResponseWrapper(OrganizationResponse response)
 {
 try
 {
 this.entity = ((RetrieveResponseWrapper)response).Entity;
 }
 catch
 {
 this.entity = ((RetrieveResponse)response).Entity;
 }
 }

/// <summary>
 /// Gets or sets the entity.
 /// </summary>
 /// <value>
 /// The entity.
 /// </value>
 public Entity Entity
 {
 get
 {
 return this.entity;
 }

set
 {
 this.entity = value;
 }
 }
 }

 

Check out this post for more details
http://www.alexanderdevelopment.net/post/2013/01/13/How-to-unit-test-C-Dynamics-CRM-interface-code-part-III

 Hope it helps.


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s