Dynamics CRM 2011 Unit Test Part 7: Microsoft Fakes with CrmOrganizationServiceContext through IOrganizationService

Dynamics CRM 2011 Unit Test Part 1: Introduction and Series Contents

The complete sample code using Microsoft Fakes can be downloaded from MSDN sample gallery: Dynamics CRM unit test using Microsoft Fakes

In a previous post (Part 4: Microsoft Fakes with CrmOrganizationServiceContext), I have describe how to use run time redirect delegate feature of Microsoft Fakes to unit test CrmOrganizationServiceContext. I consider this a direct approach since you do not need to understand the IOrganizationService API and you do not need to know all the underlining messages. If you know IOrganizationService API and underlining messages, there is also an indirect approach to unit test CrmOrganizationServiceContext, which is to mock the underlining IOrganizationService.

CrmOrganizationServiceContext is SDK Extensions contained in Microsoft.Xrm.Client assembly. CrmOrganizationServiceContext allows the entity objects produced by the data context to participate in the WCF Data Services framework. CrmOrganizationServiceContext provide methods from various OrganizationRequest, which means a method based implementation of the message API. These methods are defined as extension methods, to use these methods we have to include the Microsoft.Xrm.Client.Messages namespace.

For more information on CrmOrganizationServiceContext, see Developer Extensions Context Object Model

CrmOrganizationServiceContext will perform all its method work through IOrganizationService.Execute method. We need to provide an instance of IOrganizationService to construct OrganizationServiceContext, OrganizationServiceContext is going to translate LINQ to CRM query into RetrieveMultipleRequest and execute that OrganizationRequest by calling IOrganizationService.Execute. If we mock IOrganizationService properly, we can isolate LINQ to CRM query unit test.

Code Under Test


public class CrmContextMethods2
 {
 private CrmOrganizationServiceContext _crmContext;

public CrmContextMethods2(IOrganizationService service)
 {
 _crmContext = new CrmOrganizationServiceContext(service);
 }

public string RetrieveVersion()
 {
 var version = _crmContext.RetrieveVersion();

return version;
 }

public Guid CreateAccount(string name)
 {
 var entity = new Entity("account")
 {
 Attributes = new Microsoft.Xrm.Sdk.AttributeCollection
 {
 { "name", name }
 }
 };

var id = _crmContext.Create(entity);

return id;
 }
 }

Unit Test


[TestClass]
 public class CrmContextMethods2Test
 {
 [TestMethod]
 public void CreateAccountTest()
 {
 //
 // Arrange
 //
 string accountName = "abcabcabc";
 Guid actual;
 Guid expected = Guid.NewGuid();

int callCount = 0;
 Entity entity = null;

var service = new Microsoft.Xrm.Sdk.Fakes.StubIOrganizationService();
 service.CreateEntity = e =>
 {
 callCount++;
 entity = e;
 return expected;
 };

CrmContextMethods2 target = new CrmContextMethods2(service);

//
 // Act
 //
 actual = target.CreateAccount(accountName);

//
 // Assert
 //
 Assert.AreEqual(callCount, 1); // verify OrganizationServiceContext.Create is called once
 Assert.IsNotNull(entity); // verify OrganizationServiceContext.Create is called with not null object
 Assert.AreEqual(entity.LogicalName, "account"); // verify OrganizationServiceContext.Create is called with entity with proper entity name
 Assert.AreEqual(entity.GetAttributeValue<string>("name"), accountName); // verify OrganizationServiceContext.Create is called with entity with proper value set on name attribute

Assert.AreEqual(expected, actual);
 }

[TestMethod]
 public void RetrieveVersionTest()
 {
 //
 // Arrange
 //
 string actual;
 string expected = "5.0.9690.2243";

int callCount = 0;

var service = new Microsoft.Xrm.Sdk.Fakes.StubIOrganizationService();
 service.ExecuteOrganizationRequest = r =>
 {
 callCount++;

return new RetrieveVersionResponse
 {
 Results = new ParameterCollection
 {
 { "Version", expected }
 }
 };
 };

CrmContextMethods2 target = new CrmContextMethods2(service);

//
 // Act
 //
 actual = target.RetrieveVersion();

//
 // Assert
 //
 Assert.AreEqual(callCount, 1); // verify OrganizationServiceContext.RetrieveVersion is called once

Assert.AreEqual(expected, actual);
 }

}

Advertisements

3 comments on “Dynamics CRM 2011 Unit Test Part 7: Microsoft Fakes with CrmOrganizationServiceContext through IOrganizationService

  1. Pingback: Dynamics CRM 2011 Unit Test Part 4: Microsoft Fakes with CrmOrganizationServiceContext | Zhongchen Zhou's Blog

  2. Pingback: Dynamics CRM 2011 Unit Test Part 1: Introduction and Series Contents | Zhongchen Zhou's Blog

  3. Pingback: Information to Get started with Unit Testing with Microsoft Fakes and CRM – Hosk's Dynamic CRM Blog

Leave a Reply

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