2014-08-28 4 views
9

유창 주장을 사용하여, 나는 어느 특정 문자열이 포함되어 있는지 주장하고 싶은 두 문자열 중 하나를유창함 주장 하나 또는 다른 값을 어설는

값의도가 포함되어있는 경우에만
actual.Should().Contain("oneWay").Or().Should().Contain("anotherWay"); 
// eiter value should pass the assertion. 
// for example: "you may do it oneWay." should pass, but 
// "you may do it thisWay." should not pass 

, 주장을 실패해야합니다. Or() 연산자가 없으므로 컴파일되지 않습니다.

이것은 내가 지금 그것을 할 방법은 다음과 같습니다

이 장황
bool isVariant1 = actual.Contains(@"oneWay"); 
bool isVariant2 = actual.Contains(@"anotherWay"); 
bool anyVariant = (isVariant1 || isVariant2); 
anyVariant.Should().BeTrue("because blahblah. Actual content was: " + actual); 

, 인수가 의미있는 출력을 가지고 수동으로 만든 얻을해야한다 "때문에".

더 쉽게 읽을 수있는 방법이 있습니까? 내가 .NET 3.5 FluentAssertions에게 버전 2.2.0.0을 사용하고

이 중요한 경우 해결책은 ... Be(), HaveCount() 등 같은 다른 유창 주장 유형에 적용해야합니다.

답변

9

나는 어설 션 문자열을 확장 한 것입니다. 이런 식으로 뭔가가 :

public static void BeAnyOf(this StringAssertions assertions, string[] expectations, string because, params string[] args) { 
    Execute.Assertion 
      .ForCondition(expectations.Any(assertions.Subject.Contains)) 
      .BecauseOf(because, args) 
      .FailWith("Expected {context:string} to be any of {0}{reason}", expectations); 
} 

당신도 repository 포크과 다음 버전의 일부로 만들기 위해 Pull Request로 날을 제공 할 수 있습니다.

1

당신은 조금 더 읽기 간단한 문자열 확장을 작성하여 만들 수 :

actual.ContainsAnyOf("oneWay", "anotherWay").Should().BeTrue("because of this reason"); 

불행하게도이는 "이유"에 도움이되지 않습니다

public static class StringExt 
{ 
    public static bool ContainsAnyOf(this string self, params string[] strings) 
    { 
     return strings.Any(self.Contains); 
    } 
} 

그런 다음 당신이 할 수있는 메시지의 일부이지만 조금 나아 졌다고 생각합니다.

+1

실제로 이것이 실패하면 잘못된 점이 없습니다. –

8

이 기능을 사용하지 않아야합니까?

actual.Should().BeOneOf("oneWay", "anotherWay"); 

나를 위해 일한 v3.1.229.

+0

전체 문자열 내용을 주장하고 싶지 않아서 실제 값에 부분적으로 값 중 하나가 포함되어 있기 때문에 나를위한 것이 아닙니다. – Marcel