17

流れるようなアサーションを使用して、特定の文字列に次の 2 つの文字列のいずれかが含まれていることをアサートしたいと思います。

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

これは冗長であり、意味のある出力を得るには、「because」引数を手動で作成する必要があります。

これをより読みやすい方法で行う方法はありますか?Be()ソリューションは、HaveCount()などの他の流暢なアサーション タイプにも適用する必要があります。

問題がある場合は、.NET 3.5 で FluentAssertions バージョン 2.2.0.0 を使用しています。

4

3 に答える 3

2

単純な文字列拡張を書くことで、もう少し読みやすくすることができます:

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

次に、これを行うことができます:

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

残念ながら、これはメッセージの「理由」の部分には役に立ちませんが、少しは良くなると思います。

于 2014-08-28T08:11:28.177 に答える