如何向实体添加注释?

时间:2017-02-20 18:04:23

标签: c# .net visual-studio dynamics-crm-2011 fakexrmeasy

我正在尝试为此方法编写单元测试:

    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
    }

我的单元测试会创建2个注释,并将它们附加到销售订单:

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

在向我的实体添加注释时做错了什么?

我问的原因是因为当单元测试运行此代码时:

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

它表明虽然有1个结果,但正确的应该是;它表明没有注释与它相关联:

enter image description here

0 个答案:

没有答案