在单元测试中断言重复元素

uce*_*cef 1 c# unit-testing xunit shouldly

如何根据某些属性进行单元测试检查对象列表是否不包含重复元素。

这是我尝试做的:

[Fact]
public void RecupererReferentielContactClient_CasNominal_ResultOk()
{
     // Arange
     var contactCoreService = Resolve<IContactCoreService>();
     int clientId = 56605;
     ICollection<Personne> listPersone = new List<Personne>();

     // Act
     WithUnitOfWork(() => listPersone = contactCoreService.RecupererReferentielDeContactClient(clientId));

      // Assert
     listPersone.ShouldSatisfyAllConditions(
            () => listPersone.ShouldNotBeNull(),
            () => listPersone.ShouldBeUnique());            
}
Run Code Online (Sandbox Code Playgroud)

如何使用 shouldly 进行单元测试?

Kri*_*ica 5

按您要检查的所有属性分组,然后测试所有组是否正好有 1 个项目。

bool allUnique= listPersone
    .GroupBy(p=> new {properties you want to check})
    .All(g=>g.Count()==1);
Assert.True(allUnique)
Run Code Online (Sandbox Code Playgroud)