我正在尝试为此方法编写单元测试:
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 结果 = _xrmServiceContext.SalesOrderSet.Where(x => x.Id == orderGuid);
它表明虽然有 1 个结果,但应该是正确的;它表明没有注释与其链接: