2

NUnit 3.4.1、ジャストモック 2016.2.713.2

私はテスト中のクラスを持っています:

public class AppManager {
    public string[] GetAppSets() => Registry.LocalMachine
        .OpenSubKey(@"SOFTWARE\Autodesk\AutoCAD", false)
        ?.GetSubKeyNames();
}

また、GetAppSetsメソッドのテストがあります:

[Test]
public void GetAppSets_Returns_ValidValue() {

    const string subkey = @"SOFTWARE\Autodesk\AutoCAD";
    /* The sets of applications which are based on 
     * AutoCAD 2009-2017. */
    string[] fakeSets = new[] { "R17.2", "R18.0",
        "R18.1", "R18.2", "R19.0", "R19.1", "R20.0",
        "R20.1","R21.0" };

    RegistryKey rk = Mock.Create<RegistryKey>();

    Mock.Arrange(() => rk.GetSubKeyNames()).Returns(
        fakeSets);

    Mock.Arrange(() => Registry.LocalMachine.OpenSubKey
    (subkey, false)).Returns(rk);

    AppManager appMng = new AppManager();
    string[] appSets = appMng.GetAppSets();

    Assert.AreEqual(fakeSets, appSets);
}

できます。しかし、メソッドが "S OFTWARE\Autodesk\AutoCAD " の代わりに " Software\Autodesk\AutoCAD " または " software\autodesk\autocadGetAppSets " 文字列を使用する場合、私のテストは失敗します:文字列の大文字と小文字が変更される場合、変数はキーがコンピュータに存在しません)。appSetsnull

したがって、この場合テスターはGetAppSetsメソッドの実装 (悪いバリアント)を知るか、大文字と小文字を区別しない文字列のようなパラメーターを処理する必要があります。

2番目のバリアントを使用することは可能ですか?

4

2 に答える 2

1

元の質問への回答:

等値アサーションのオーバーロードされたバージョンを使用できます。

Assert.AreEqual(fakeSets, appSets, true);

サイン:

public static void AreEqual(
string expected,
string actual,
bool ignoreCase)

ソース: https://msdn.microsoft.com/en-us/library/ms243448.aspx

更新された質問への回答:

for(int i = 0; i < appSets.Length, i++)
{   // If there is mismatch in length Exception will fail the test.
    Assert.AreEqual(fakeSets[i], appSets[i], true);
}
于 2016-08-09T10:06:11.467 に答える