0

I am attempting to write a unit test for this method:

    public List<Annotation> GetNotesByOrderGuid(Guid orderGuid)
    {
        var result = _xrmServiceContext.SalesOrderSet.Where(x => x.Id == orderGuid); //!!!!! this is returning correctly 1 record, however, it shows NULL for the list of annotations
        //do some stuff and return a list of annotations
    }

My unit test creates 2 notes, and attaches them to the sales order:

    private XrmFakedContext _fakeContext;

    [NotNull]
    private IOrganizationService _fakeOrganizationService;

    [Test]
    public void GetNotesByOrderGuid_ExistingRecordHavingNotes_ReturnsListOfThoseNotes()
    {
        using (var xrmServiceContext = new XrmServiceContext(_fakeOrganizationService))
        {
            // Arrange
            var salesOrderGuid = Guid.NewGuid();
            var salesOrder = new SalesOrder { Id = salesOrderGuid };
            var note1 = new Annotation
                            {
                                Id = Guid.NewGuid(),
                                NoteText = "this is note1",
                                ObjectId = new EntityReference(SalesOrder.EntityLogicalName, salesOrderGuid),
                                ObjectTypeCode = salesOrder.LogicalName
                            };
            var note2 = new Annotation
                            {
                                Id = Guid.NewGuid(),
                                NoteText = "this is note2",
                                ObjectId = new EntityReference(SalesOrder.EntityLogicalName, salesOrderGuid),
                                ObjectTypeCode = salesOrder.LogicalName
                            };

            _fakeContext.Initialize(new List<Entity> { salesOrder, note1, note2});
            var sut = new SalesOrderService(xrmServiceContext);

            // Act
            // Assert
            Assert.That(sut.GetNotesByOrderGuid(salesOrderGuid), Is.InstanceOf<List<Annotation>>());
        }
    }

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

What am doing incorrectly in adding annotations to my entity?

The reason I ask is because when the unit tests runs this code:

var result = _xrmServiceContext.SalesOrderSet.Where(x => x.Id == orderGuid);

it shows that although there is 1 result, as there correctly should be; it shows that no annotations have been linked to it:

enter image description here

Alex Gordon
  • 51,480
  • 273
  • 609
  • 976
  • Related entities are not automatically retrieved. You have to request them as part of the query. I don't know if that is possible using LINQ. It can be done using `QueryExpression` (http://www.inogic.com/blog/2011/09/retrieve-related-entity-records-along-wih-the-primary-entity-using-retrieve-method/). That said, if it is a LINQ query you want just join to Annotations and create a new object. – Nicknow Feb 20 '17 at 22:55
  • Nick is right. You need to do a join with the Annotation entity, by default it's gonna pull salesorder records only. – Jordi Mar 06 '17 at 22:24

0 Answers0