How to do it...

  1. Create a new unit test project under your existing solution called Packt.Xrm.UnitTest.
  2. Add a reference to the Moq NuGet package as well as Microsoft.CrmSdk.CoreAssemblies.
  3. Add a reference to your existing Packt.Xrm.Extensions (refactored) project.
  4. Create a new Basic Unit Test class called EmailUpdateBusinessLogicTest.
  1. Add the following using statements:
using Packt.Xrm.Extensions.BusinessLogic; 
using Packt.Xrm.Extensions.DataAccessLayer;
using Moq;
using Packt.Xrm.Entities;
using Microsoft.Xrm.Sdk;
using System.Collections.Generic;
  1. Add the following private instance variables:
private Mock<IEmailDataAccessLayer> _emailDalMock; 
private Mock<ICustomTracingService> _tracing;
  1. Create a private SetupMocks method with the following implementation:
private void SetupMocks() 
{
_emailDalMock = new Mock<IEmailDataAccessLayer>();
_emailDalMock.Setup(dal => dal.GetEmails(It.IsAny<Guid>
())).Returns(
new List<Email>() { new Email() { Subject = "sample1"},
new Email { Subject = "sample2" },
new Email { Subject = string.Empty } }
);
_tracing = new Mock<ICustomTracingService>();
}
  1. Create a TestUpdateEmail test method with the following implementation:
[TestMethod] 
public void TestUpdateEmail()
{
//Arrange
SetupMocks();

var businessLogic = new UpdateEmailLogic(_emailDalMock.Object, _tracing.Object);

//Act
businessLogic.UpdateAccountsEmails(Guid.NewGuid());

//Assert
_emailDalMock.Verify(dal => dal.CloseEmailAsCancelled(It.IsAny<Email>()), Times.Exactly(1));
_emailDalMock.Verify(dal => dal.UpdateEntity(It.IsAny<Entity>()), Times.Exactly(2));
_tracing.Verify(trace => trace.Trace("{0} closed emails and {1} updated emails", 1, 2), Times.Exactly(1));
}
..................Content has been hidden....................

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