2016-08-31 4 views
-1

私はユニットテストの初心者です。私の会社はNUnitを使い、作成したサービスメソッドでヌルチェックをテストしようとしています。 string acctName = ""をテストしようとすると、私のAssertステートメントはどのように見えるはずですか?何らかの理由でstring acctNameがコンパイルエラーを受け取りました。NUnitを使用したヌルチェックのテスト問題

"この名前は現在のコンテキストに存在しません。"

MY METHOD:

public Dict getOrder(Client client) 
{ 
    string acctName = client != null ? client.AccountName : ""; 

    Dict replacements = new Replacement 
    { 
     {COMPANY_NAME, acctName} 
    }; 
    return new Dict(replacements); 
} 

MY TEST:

public void getOrderNullTest() 
{ 

    //Arrange 

    Client myTestClient = null; 

    //Act 

    contentService.getOrder(myTestClient); 

    //Assert 

    Assert.AreEqual(string acctName, ""); 

} 
+0

あなたはコンパイラエラーが何であるかを含める必要があります書かれている可能性が別の方法です。 – hatchet

+0

また、 'Client.AccountName'のタイプは何ですか?あなたはそれが 'string'だと確信しています、そして、そのプロパティはクラスによって定義されていますか? – hatchet

+0

エラーを追加しました。はい、ビジュアルスタジオのおかげでタイプストリングです;) –

答えて

0

ながら電子あなた自身の質問に答えて、それを動作させました。問題がアブストラクトを呼び出す際に、に構文エラー(string acctName)を持っていることがわかりました。ここで

は、あなたがそれを

//Arrange 
Client myTestClient = null; 
string expectedValue = String.Empty; 
string expectedKey = COMPANY_NAME; 

//Act 
Dict result = contentService.getOrder(myTestClient); 

//Assert 
Assert.IsNotNull(result); 

string actualValue = result[expectedKey]; 

Assert.IsNotNull(actualValue); 
Assert.AreEqual(expectedValue, actualValue); 
+0

@Nkosiの回答と説明をありがとうが、expectedKeyは文字列 'actualValue = result [expectedKey];'? –

+0

Dictは、そのキーに格納されている値を返すキーでインデックス付き呼び出しを許可するディクショナリ型であるという前提がありました。 – Nkosi

+0

私はちょうど新しいことを学びました。 :)私はインデックス呼び出しが[0]または[1]のように値を取得するためにインデックス番号を参照しなければならないと考えました。 –

1

私はこのようにそれを書いてしまった:

//Arrange 

Client myTestClient = null; 
string expectedValue = String.Empty; 
string expectedKey = COMPANY_NAME; 

//Act 

Dict actual = contentService.getOrder(myTestClient); 

//Assert 

Assert.IsTrue(actual.ContainsKey(expectedKey)); 
Assert.IsTrue(actual.ContainsValue(expectedValue)); 
関連する問題