2017-04-12 7 views
0

NUnitの3、VS2015
は、私が使用してTestCase属性を通じて私の方法の例外タイプをテストしたいのですが、VS2015での私のテストをNUnit3TestAdapter見ていない(私のクラスはpublicです):私のメソッドの例外のセットは、1回のテストだけでどのようにテストできますか?

[TestCase(null, typeof(ArgumentNullException))] 
[TestCase("", typeof(ArgumentException))] 
[TestCase(" ", typeof(ArgumentException))] 
[TestCase(" \t \t ", typeof(ArgumentException))] 
[TestCase("< !!!>", typeof(FileNotFoundException))] 
public void InvalidFilePath_ThrowsException<T>(string name, 
    T exception_type) where T : Exception { 

    Dictionary<string, string> dict = new Dictionary<string, string>(); 

    Assert.Throws<T>(() => ModelFactory.Current.CreateDocument(name, 
     dict)); 
} 

さらに:でこの場合NUnit3TestAdapterが私のすべてのテストを見ていない...しかし、私はこのテストをコメントする場合は、NUnit3TestAdapterは、他のテストを見ている:

// [TestCase(null, typeof(ArgumentNullException))] 
// [TestCase("", typeof(ArgumentException))] 
// [TestCase(" ", typeof(ArgumentException))] 
// [TestCase(" \t \t ", typeof(ArgumentException))] 
// [TestCase("<!!!>", typeof(FileNotFoundException))] 
// public void InvalidFilePath_ThrowsException<T>(string name, 
// T exception_type) where T : Exception { 

// Dictionary<string, string> dict = new Dictionary<string, string>(); 

// Assert.Throws<T>(() => ModelFactory.Current.CreateDocument(name, 
//  dict)); 
//} 

[Test] 
public void InvalidFilePath_ThrowsException_01() { 

    Dictionary<string, string> dict = new Dictionary<string, string>(); 

    Assert.Throws<ArgumentNullException>(() => ModelFactory.Current 
    .CreateDocument(null, dict)); 
} 

[Test] 
public void InvalidFilePath_ThrowsException_02() { 

    Dictionary<string, string> dict = new Dictionary<string, string>(); 

    Assert.Throws<ArgumentException>(() => ModelFactory.Current 
    .CreateDocument("", dict)); 
} 

[Test] 
public void InvalidFilePath_ThrowsException_03() { 

    Dictionary<string, string> dict = new Dictionary<string, string>(); 

    Assert.Throws<ArgumentException>(() => ModelFactory.Current 
    .CreateDocument(" ", dict)); 
} 

[Test] 
public void InvalidFilePath_ThrowsException_04() { 

    Dictionary<string, string> dict = new Dictionary<string, string>(); 

    Assert.Throws<ArgumentException>(() => ModelFactory.Current 
    .CreateDocument(" \t \t ", dict)); 
} 

[Test] 
public void InvalidFilePath_ThrowsException_05() { 

    Dictionary<string, string> dict = new Dictionary<string, string>(); 

    Assert.Throws<FileNotFoundException>(() => ModelFactory.Current 
    .CreateDocument("<!!!>", dict)); 
} 

は、どのように私はそれを解決することができますか?私は5つの別々のテストを作成したくないです...

+0

ルック:http://stackoverflow.com/questions/801153/parametric-test-with-generic-methods – vyrp

+0

どのようにその情報は私のケースで私を助けることができますか? –

+0

回答を投稿しました – vyrp

答えて

1

私は問題がテストメソッドが一般的だと思っています。代わりに、一般的なAssert.Throws<T>を使用するのではなく、例外タイプを受け入れるオーバーロードを使用:この質問で

[TestCase(null, typeof(ArgumentNullException))] 
[TestCase("", typeof(ArgumentException))] 
[TestCase(" ", typeof(ArgumentException))] 
[TestCase(" \t \t ", typeof(ArgumentException))] 
[TestCase("< !!!>", typeof(FileNotFoundException))] 
public void InvalidFilePath_ThrowsException(string name, Type exceptionType) 
{ 
    Dictionary<string, string> dict = new Dictionary<string, string>();  
    Assert.Throws(exceptionType,() => ModelFactory.Current.CreateDocument(name, dict)); 
} 
+0

良い答えですが、ジェネリック版がうまくいきません。例外のタイプが最初に来るように引数を逆にしてみてください。私は何が起こるか知りたいと思っています。 – Charlie

+0

@Charlie:私はフィドルに時間があるときにそのショットを与える必要があります... –

+0

ああ!気にしないで。このメソッドは例外のインスタンスではなく、例外のTypeを取るため、ジェネリックの有効な使用ではありません。簡単な間違いであることが分かります。 :-) – Charlie

関連する問題