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();
}
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
.