How to do it...

  1. Add a new Basic Unit Test class called InMemoryEmailUpdateTest.cs to your unit test project.
  2. Add a reference to the following NuGet packages:
    • Microsoft.CrmSdk.CoreAssemblies
    • XrmUnitTest.2016
    • Moq
  3. Ensure that you have a reference to the Packt.Xrm.Extensions project.
  1. Add the following using statements:
using Packt.Xrm.Extensions.Plugin; 
using Moq;
using Microsoft.Xrm.Sdk;
using Packt.Xrm.Entities;
using DLaB.Xrm.LocalCrm;
using DLaB.Xrm.Test;
  1. Add the following private instance variables:
private const int NumberOfDaysDifference = 10; 
private IServiceProvider _serviceProvider;
private Mock<ITracingService> _tracingMock;
private IOrganizationService _organizationServiceFake;
private Guid _email1Guid;
private Guid _email2Guid;
private Guid _email3Guid;
  1. Add the following private SetupMock method to set up your fakes and mocks:
private void SetupMock() 
{
var serviceProviderMock = new Mock<IServiceProvider>();
_tracingMock = new Mock<ITracingService>();

_organizationServiceFake = new LocalCrmDatabaseOrganizationService(LocalCrmDatabaseInfo.Create<OrganisationServiceContext>());
var accountGuid = _organizationServiceFake.Create(new Account() { Name = "Packt Test" });
_email1Guid = _organizationServiceFake.Create(new Email() { Subject = "Mock 1", RegardingObjectId = new EntityReference(Account.EntityLogicalName, accountGuid) });
_email2Guid = _organizationServiceFake.Create(new Email() { Subject = "Mock 2", RegardingObjectId = new EntityReference(Account.EntityLogicalName, accountGuid) });
_email3Guid = _organizationServiceFake.Create(new Email() { Subject = string.Empty, RegardingObjectId = new EntityReference(Account.EntityLogicalName, accountGuid) });

var pluginContextMock = new Mock<IPluginExecutionContext>();
var parameterCollection = new ParameterCollection();
parameterCollection.Add("Target", new Account() { Id = accountGuid });
pluginContextMock.Setup(c => c.InputParameters).Returns(parameterCollection);
var organisationServicefactoryMock = new Mock<IOrganizationServiceFactory>();
organisationServicefactoryMock.Setup(f => f.CreateOrganizationService(It.IsAny<Guid>())).Returns(_organizationServiceFake);
serviceProviderMock.Setup(sp => sp.GetService(typeof(ITracingService))).Returns(_tracingMock.Object);
serviceProviderMock.Setup(sp => sp.GetService(typeof(IPluginExecutionContext))).Returns(pluginContextMock.Object);
serviceProviderMock.Setup(sp => sp.GetService(typeof(IOrganizationServiceFactory))).Returns(organisationServicefactoryMock.Object);

_serviceProvider = serviceProviderMock.Object;
}
  1. Create the following test method:
[TestMethod] 
[TestCategory("InMemory")]
public void Fake()
{
//Arrange
SetupMock();

var plugin = new UpdateActivity();
//Act
plugin.Execute(_serviceProvider);

//Assert
Assert.AreEqual(EmailState.Open,
_organizationServiceFake.GetEntity(new Id<Email>
(_email1Guid)).StateCode);
Assert.AreEqual(DateTime.Now.Date.AddDays
(NumberOfDaysDifference), _organizationServiceFake.GetEntity
(new Id<Email>(_email1Guid)).ScheduledStart,
"Start date not updated correctly for emails with a subject");
Assert.AreEqual(EmailState.Open,
_organizationServiceFake.GetEntity(new Id<Email>
(_email2Guid)).StateCode);
Assert.AreEqual(DateTime.Now.Date.AddDays
(NumberOfDaysDifference),
_organizationServiceFake.GetEntity(new Id<Email>
(_email2Guid)).ScheduledStart, "Start date not updated
correctly for emails with a subject");
Assert.AreEqual(EmailState.Canceled,
_organizationServiceFake.GetEntity(new Id<Email>
(_email3Guid)).StateCode, "Email without a subject was not
cancelled");
}
  1. Run your unit test in debug mode.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.42.164