문제

TDD와 Xunit을 처음 사용하므로 다음과 같은 모습을 테스트하고 싶습니다.

List<T> DeleteElements<T>(this List<T> a, List<T> b);

내가 사용할 수있는 주장 방법이 있습니까? 나는 이런 것이 좋을 것이라고 생각합니다

    List<int> values = new List<int>() { 1, 2, 3 };
    List<int> expected = new List<int>() { 1 };
    List<int> actual = values.DeleteElements(new List<int>() { 2, 3 });

    Assert.Exact(expected, actual);

이런 것이 있습니까?

도움이 되었습니까?

해결책

xunit.net 컬렉션을 인식하므로 그냥해야합니다

Assert.Equal(expected, actual); // Order is important

다른 컬렉션 어설 션을 볼 수 있습니다 CollectionAsserts.cs

을 위한 NUNIT 도서관 수집 비교 방법입니다

CollectionAssert.AreEqual(IEnumerable, IEnumerable) // For sequences, order matters

그리고

CollectionAssert.AreEquivalent(IEnumerable, IEnumerable) // For sets, order doesn't matter

자세한 내용은 여기를 참조하십시오. CollectionAssert

mbunit 또한 Nunit과 유사한 수집 어설 션이 있습니다. assert.collections.cs

다른 팁

현재 버전의 Xunit (1.5)에서만 사용할 수 있습니다.

Assert.equal (예상, 실제);

위의 방법은 두 목록을 요소 비교하여 요소를 수행합니다. 이것이 사전 버전에서 작동하는지 확실하지 않습니다.

Xunit을 사용하면 각 요소의 체리를 선택하여 테스트하기 위해 Assert.collection을 사용할 수 있습니다.

Assert.Collection(elements, 
  elem1 => Assert.Equal(expect1, elem1.SomeProperty),
  elem2 => { 
     Assert.Equal(expect2, elem2.SomeProperty);
     Assert.True(elem2.TrueProperty);
  });

이것은 예상 수를 테스트하고 각 조치가 확인되도록합니다.

최근에 나는 사용하고 있었다 xUnit 2.4.0 그리고 Moq 4.10.1 내 ASP.NET Core 2.2 앱의 패키지.

제 경우에는 두 단계 프로세스에서 작동하도록했습니다.

  1. 구현 정의 IEqualityComparer<T>
  2. 비교 인스턴스를 세 번째 매개 변수로 전달하십시오 Assert.True 방법:

    Assert.True(expected, actual, new MyEqualityComparer());

그러나 동일한 결과를 사용하여 더 좋은 방법이 있습니다. 유동성 분석 패키지. 다음을 수행 할 수 있습니다.

// Assert          
expected.Should().BeEquivalentTo(actual));

흥미롭게도 Assert.Equal() 두 목록의 요소를 주문하여 같은 순서로 가져 오기 위해 항상 실패합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top