FakeXRMEasy

FAKEXRMEASY

Install Specflow in Visual Studio. Only first time

Go to:

  • Tools > Extensions and Updates.
  • Look for SpecFlow for Visual Studio 2017 and install it.

Configure FakeXrmEasy + Specflow

  1. Create a new project Unit Test Project (.NET Framework) with the version 4.7.2
    • Probably you have to put a shorter name, (depending on location and name of project/assembly)
  2. Then follow these steps. In order to install the packages go to:
    • Tools > NuGet Package Manager > Package Manager Console
    • In the default project select your new project created
Install-Package Microsoft.CrmSdk.CoreAssemblies
Install-Package SpecFlow
Install-Package SpecFlow.NUnit
Install-Package SpecFlow.Tools.MsBuild.Generation
Install-Package FakeXrmEasy.9
Install-Package NUnit.ConsoleRunner
Install-Package NUnit3TestAdapter
  • Go to Manage Nuget Packages (Right Click on the project)
  • Update all the installed apart from the FakeItEasy

---- Create specflow file. Add new item > Visual C# Items > SpecFlow > Specflow Feature File. DO NOT select the ones that come with language.

  • Build
  • Add the feature.cs file created into the project
  • Add a reference of the main project
  • Add a reference to the project that contains your Entities delivered from CrmSvcUtil

For adding the link existing item:

  1. Remove the class UnitTest1.cs
  2. Right click on the Test project
  3. Add existing item
  4. Select the file
  5. In the button that says [Add] click on the arrow on its right
  6. Select [Add as a link]

  • Add as a link existing item of …\YourProject_Tests\Steps\Generic_Steps.cs
  • Add as a link existing item of …\YourProject_Tests\MockCRM.cs
  • Add as a link existing item of …\YourProject_Tests\XrmContext.cs

Generating When steps

The text on the .feature file will be purple

  1. Click with the right button and click Generate Step Definitions
  2. Select only the When step, deselect the others
  3. In the class name put When_Steps (Name can be whatever but let’s go with the same name always)
  4. Click the button [Generate]

Inside use the following code:

XrmContext CRM { get; set; }
IOrganizationService service { get; set; }
public When_Steps()
{
    CRM = Generic_Steps.CRM;
    service = Generic_Steps.service;
}
[When(...)]
public void ...{

}

Create Metadata

For creating metadata it is needed to initialise the metadata as assembly (attributes are not coming) or you can create the metadata yourself. For creating the entity metadata with some attributes you can pass the attribute.

Create Primary Attribute Name

For creating the primary attribute name metadata it has been created a function that will add the attribute logical name in the metadata of the entity.

public void AddPrimaryAttributeNameMetadataToMock(string entityLogicalName, string attributeLogicalName)
{
    var metadata = FakeXrmContext.GetEntityMetadataByName(entityLogicalName);

    var nameAttribute = new StringAttributeMetadata()
    {
        LogicalName = attributeLogicalName,
        RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired),
    };

    metadata.SetAttributeCollection(new[] { nameAttribute });
    metadata.SetFieldValue("_primaryNameAttribute", attributeLogicalName);
    FakeXrmContext.SetEntityMetadata(metadata);
}

The code that has been added in the Specflow has been:

[Given(@"the entity name contains primary attribute name")]
public void GivenEntityNameWithPrimaryNameField(Table table)
{
    foreach (var row in table.Rows)
    {
        var entity = row["Field"];
        var attribute = row["Value"];
        CRM.AddPrimaryAttributeNameMetadataToMock(entity,attribute);
    }
}

Create option set into the metadata

It has been created a functionality to add an option set with several labels to the metadata

public void AddOptionSetMetadataToMock(string entityLogicalName, string attributeSchemaName, string attributeLogicalName,
            string attributeDisplayName,
            List<KeyValuePair<string, int>> localizedLabels, int languageCode = 1033,
            AttributeRequiredLevel requiredLevel = AttributeRequiredLevel.SystemRequired,
            OptionSetType enumType = OptionSetType.Picklist, bool isGlobal = false)
{
    var metadata = FakeXrmContext.GetEntityMetadataByName(entityLogicalName);

    // Craeate the options
    OptionMetadataCollection optionsCollection = new OptionMetadataCollection();
    foreach (var optMetadata in localizedLabels)
    {
        var label = new Label(optMetadata.Key, languageCode);
        optionsCollection.Add(new OptionMetadata(label, optMetadata.Value));
    }
    var optionsMetadata = new OptionSetMetadata(optionsCollection);
    optionsMetadata.IsGlobal = false;
    optionsMetadata.OptionSetType = enumType;

    metadata.SetAttribute(new PicklistAttributeMetadata
    {
        SchemaName = attributeSchemaName,
        LogicalName = attributeLogicalName,
        DisplayName = new Label(attributeDisplayName, languageCode),
        RequiredLevel = new AttributeRequiredLevelManagedProperty(
            requiredLevel),
        OptionSet = optionsMetadata
    });

    FakeXrmContext.SetEntityMetadata(metadata);
}

Create a relationship N:N

public void AddRelationshipNN(string namerelationship, string intersectentityname, string entity1logicalname, string entity2logicalname, string entity1attributename, string entity2attributename)
{
    FakeXrmContext.AddRelationship(namerelationship, new XrmFakedRelationship
    {
        IntersectEntity = intersectentityname,
        Entity1LogicalName = entity1logicalname,
        Entity1Attribute = entity1attributename,
        Entity2LogicalName = entity2logicalname,
        Entity2Attribute = entity2attributename
    });
}

Typical errors

Type Object has not been mapped to an AttributeMetadata

What you have to do is - in the XrmContext entities- create the entity with the XrmContext generator tool


Join the newsletter