Natasha Jovanoski

Natasha Jovanoski

  • 1.7k
  • 3
  • 402

Unit tests for the workflow on CRM

Oct 17 2023 12:35 PM

Can someone help me, I will write for the first time unit tests for the workflow on CRM, i found some help here,

using CodeActivityGenus;
using Microsoft.QualityTools.Testing.Fakes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Fakes;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Xrm.Sdk.Workflow.Fakes;
using System;
using System.Activities;
using System.Diagnostics;

namespace UnitTestProject1
{
    [TestClass]
    public class CustomActivityCancelSingleOrderLineTest
    {
        [TestMethod]
        public void TestCustomActivityCancelSingleOrderLine()
        {
            using (ShimsContext.Create())
            {
                // Stub IOrganization Service object
                var service = new StubIOrganizationService();

                // Stub WorkflowContext Service object
                var workflowContext = new StubIWorkflowContext();

                // Stub TracingService Service object
                var tracingService = new StubITracingService();

                // Stub IOrganization Service Factory object
                var factory = new StubIOrganizationServiceFactory();

                // Set up context
                var workflowUserId = Guid.NewGuid();
                var orderLineRef = new EntityReference("salesorderdetail", Guid.NewGuid());

                // Set up context methods
                workflowContext.UserIdGet = () => workflowUserId;

                // ITracingService
                tracingService.TraceStringObjectArray = (f, o) =>
                {
                    Debug.WriteLine(f, o);
                };

                // IOrganizationServiceFactory
                factory.CreateOrganizationServiceNullableOfGuid = id =>
                {
                    return service;
                };

                // Add your specific context setup here if needed

                // Create an instance of your code activity
                var customActivity = new CustomActivityCancelSingleOrderLine();

                // Set the inputs for the activity
                customActivity.inOrderLine = orderLineRef;

                // Execute the code activity
                customActivity.Execute(new CodeActivityContext
                {
                    WorkflowContext = workflowContext,
                    TracingService = tracingService,
                    ExecutionContext = new FakeLocalWorkflowExecutionContext(),
                });

                // Add your assertions here to verify the behavior of your code activity
                // For example, you can use service.Assert... methods to check service interactions.

                // Example assertion:
                service.AssertMethod(service.UpdateEntityReferenceColumnSetOfEntity = (entityRef, colSet, entity) =>
                {
                    // Verify that the Update method was called with the expected parameters
                    Assert.AreEqual("salesorderdetail", entityRef.LogicalName);
                    Assert.AreEqual(orderLineRef.Id, entityRef.Id);
                    Assert.AreEqual(100000005, entity.GetAttributeValue<OptionSetValue>("genus_portalorderstatus").Value);
                });
            }
        }
    }
}

but i dont know which nuget i need to install, because i have a lot of errors in this code


Answers (2)