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("genus_portalorderstatus").Value);
});
}
}
}
}
but i dont know which nuget i need to install, because i have a lot of errors in this code
Kiran GirasePosted Oct 19, 2023, 8:35 AM
Hi Natasha, go through my blog https://www.c-sharpcorner.com/blogs/unit-testing-using-fakes-for-ms-dynamics-crm-custom-workflow
you will understand how to write Unit test for custom workflow. If you understand or not reply to my blog.
Muhammad Imran AnsariPosted Oct 17, 2023, 9:49 PM
Based on the code you provided, it seems you are using Microsoft Fakes for creating stubs and shims for your CRM objects. Here are the necessary NuGet packages you may need to install for this code:
Microsoft.CrmSdk.CoreAssemblies:
This package contains the Microsoft Dynamics CRM SDK core assemblies. You can use it to work with Dynamics CRM data and metadata.
Microsoft.QualityTools.Testing.Fakes:
This package provides the necessary libraries for creating stubs and shims in your unit tests.
To make sure to install the appropriate versions compatible with your project. Here is an example of how to install these packages using the NuGet Package Manager Console in Visual Studio:
Install-Package Microsoft.CrmSdk.CoreAssemblies -Version
Install-Package Microsoft.QualityTools.Testing.Fakes -Version