creating linked data with fakexrmeasy

1.7k views Asked by At

How do you create an entity and link another entity to it in FakeXrmEasy?

I am attempting to test this code:

    public List<abc_OrderTask> GetTasks(Guid workOrderGuid)
    {
        var result = (from task in _xrmServiceContext.abc_OrderTaskSet
                      join workOrder in _xrmServiceContext.abc_workorderSet 
                                                on task.RegardingObjectId.Id equals workOrder.Id
                      where workOrder.Id == workOrderGuid
                      select task).ToList();

        return result;
    }

Relationship between abc_OrderTask and abc_WorkOrder is N:1

In my test, I am attempting to link the two entities:

    [Test]
    public void GetTasks_WorkOrderWithExistingTasks_ReturnsListOfTasks()
    {
        using (var xrmServiceContext = new XrmServiceContext(_fakeOrganizationService))
        {
            var workOrderGuid = Guid.NewGuid();
            var taskGuid = Guid.NewGuid();
            var workOrder = new abc_workorder { Id = workOrderGuid };
            var task = new abc_OrderTask
                           {
                               Id = taskGuid,
                               Subject = "Required subject",
                               RegardingObjectId =
                                   new EntityReference(abc_workorder.EntityLogicalName, workOrderGuid)
                           };
            _fakeContext.Initialize(new List<Entity> { workOrder, task });

            var sut = new WorkOrderService(xrmServiceContext);

            // Act
            // Assert
            Assert.That(sut.GetTasks(workOrderGuid), Is.InstanceOf<List<abc_OrderTask>>());
            Assert.That(sut.GetTasks(workOrderGuid).Count.Equals(1));
        }
    }

However, the result set is empty.

How do you create an entity and link another entity to it in FakeXrmEasy?

Here's how this object is getting new-ed up:

    private IOrganizationService _fakeOrganizationService;

    [SetUp]
    public void Init()
    {
        _fakeContext = new XrmFakedContext { ProxyTypesAssembly = Assembly.GetAssembly(typeof(abc_workorder)) };
        _fakeOrganizationService = _fakeContext.GetFakedOrganizationService();
    }
3

There are 3 answers

1
Nicknow On BEST ANSWER

abc_OrderTask is a custom activity. Activities are child records of their regarding object (abc_workorder), defined by abc_OrderTask.RegardObjectId. It appears this is setup correctly in your test data.

The method being tested, GetTasks, is querying based on a custom N:1 relationship from abc_workorder to abc_OrderTask with Lookup field named abc_workorder_abc_OrderTasks.

You need to fix GetTasks to filter abc_OrderTask by RegardingObjectId.Id.

0
Daryl On

Two options:

  1. Your WorkOrderService has a bug in it, and your unit test is working flawlessly.

  2. The FakeXrmEasy framework has a bug in it. I'd change your test just to retrieve the abc_OrderTask and verify that it has a RegardingObjectId. If it does, but the join isn't working, then submit your findings on the GitHub page as a bug.

0
Jordi On

Thanks you all for all the answers :)

This is gonna be a lot quicker & easier to answer by raising an issue with a unit test on the GitHub page so that we could build and run the unit test and see what's going on.

Anyway, I'd say Nicknow's is the correct answer as you are trying to filter records in the LINQ expression using the 1->N property rather than what I used to do, which is exactly the opposite, filtering by the associated EntityReference (RegardingObjectId.Id) like Nick was suggesting.

Basically you can filter 1:N queries by using the associated lookup, like contact.ParentCustomerId.Id, etc. And N:N records by using the intersect entity, with their associated lookups as well. That should work for LINQ, QueryExpressions, and FetchXml.

Hope this helps!

[EDIT]: As FakeXrmEasy is also itself unit tested, you'll find loads of different query examples here