Part 6: Microsoft Fakes with OrganizationServiceContext 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 3: Microsoft Fakes with OrganizationServiceContext), I have describe how to use run time redirect delegate feature of Microsoft Fakes to unit test OrganizationServiceContext. 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 OrganizationServiceContext, which is to mock the underlining IOrganizationService.

We need to provide an instance of IOrganizationService to construct OrganizationServiceContext, if we mock IOrganizationService properly, we can isolate OrganizationServiceContext unit test.

Code under test


public class ContextMethods2
 {
 private OrganizationServiceContext _context;

public ContextMethods2(IOrganizationService service)
 {
 _context = new OrganizationServiceContext(service);
 }

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

_context.AddObject(entity);

var results = _context.SaveChanges();

return entity.Id;
 }
 }

Unit Test


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

int callCount = 0;
 Entity entity = null;

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

var request = r as CreateRequest;
 entity = request.Target;
 expected = entity.Id;

return new CreateResponse
 {
 Results = new ParameterCollection
 {
 { "id", expected }
 }
 };
 };

ContextMethods2 target = new ContextMethods2(service);

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

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

Assert.AreEqual(expected, actual);
 }
 }

Advertisements

3 comments on “Part 6: Microsoft Fakes with OrganizationServiceContext through IOrganizationService

  1. Pingback: Dynamics CRM 2011 Unit Test Part 3: Microsoft Fakes with OrganizationServiceContext | 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