2

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();
    }
Alex Gordon
  • 51,480
  • 273
  • 609
  • 976
  • Where is `_fakeContext` coming from? How is it related to `_fakeOrganizationService`? I would add the code where you set these up. I have some examples at https://github.com/nicknow/BloggingCrm-Dynamics-Crm-Plugin-Unit-Testing-Example/blob/master/BloggingCrm.Account.BusinessLogic.Tests/Prove_that_AccountQueries_Is_Working.cs of how I used FakeXrmEasy to do this type of testing. – Nicknow Feb 15 '17 at 19:56
  • thanks @Nicknow i added that to the question – Alex Gordon Feb 15 '17 at 20:22
  • thank you but i wasnt able to find a helpful example in your github – Alex Gordon Feb 15 '17 at 20:24
  • did you try also this: where workOrder.abc_workorderId == workOrderGuid instead of where workOrder.Id == workOrderGuid? CRM LINQ doesn't know about .Id properties, only crm fields, so you'll need to use the PK there. – Jordi Feb 16 '17 at 17:43
  • Same would apply to the join : on task.RegardingObjectId.Id equals workOrder.abc_workorderId – Jordi Feb 16 '17 at 17:45

3 Answers3

4

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.

Nicknow
  • 7,064
  • 2
  • 17
  • 34
3

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

Jordi
  • 1,440
  • 9
  • 9
-1

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.

Daryl
  • 17,774
  • 8
  • 63
  • 133