2017-02-13 6 views
0

このテスト関数の先頭行がDataMisalignedExceptionをスローするようにテストします(using Microsoft.VisualStudio.TestTools.UnitTesting)。2つのパラメータを持つ関数をラムダのアクションに変換する

namespace xxx.Services.SupplierApiTests 
{ 
    [TestClass] 
    public class JsonSchemaValidatorTests 
    { 
     [TestMethod] 
     public void ShouldThrowOnBadPhoneNumber() 
     { 
      JsonSchemaValidator.validateAgainstJsonSchema(ProviderService.getErronousProviders(), "./provider-schema.json"); 
      Action<IList, string> myAction = (x, y) => JsonSchemaValidator.validateAgainstJsonSchema(x, y); 
      Assert.ThrowsException<DataMisalignedException>(myAction); 
     } 
    } 
} 

どのように私はテストの一番上の行から2つの引数に渡し、アクションとしてJsonSchemaValidator.validateAgainstJsonSchema使用することができますか?私の試みは上記のコードにありますが、2つのパラメータを渡すことはありません。

+0

私は()()=> JsonSchemaValidator.validateAgainstJsonSchema(ProviderService.getErronousProviders()、 "./provider-schema.json")あなたは 'Assert.ThrowsException したいと思います'が、私はできませんよ'Assert.ThrowsException'のためのドキュメントを見つけるために、私はたいていtestメソッドの' ExpectedException'属性を自分で使います。 – juharr

+0

@juharrはい、ありがとう、私が探していたものです。私はそれが関数を変換していないのを見て、それは無名関数の中でそれを呼んでいます。 Cheers – BeniaminoBaggins

+0

私の推測では、 'Assert.ThrowsException'は' Action'を期待し、 'Action 'は期待していません。 – juharr

答えて

1

テストメソッドの実行中に例外が予想されることを示すには、テストメソッドの上に[ExpectedException]属性を使用できます。

[TestClass] 
public class JsonSchemaValidatorTests 
{ 
    [TestMethod] 
    [ExpectedException(typeof(DataMisalignedException))] 
    public void ShouldThrowOnBadPhoneNumber() 
    { 
     JsonSchemaValidator.validateAgainstJsonSchema(ProviderService.getErronousProviders(), "./provider-schema.json"); 
    } 
} 
関連する問題