2015-11-24 5 views
31

.NET FrameworkでTesting Toolsを使用するのが初めてのため、ReSharperのヘルプを使用してNuGetからダウンロードしました。nUnitのExpectedExceptionでエラーが発生しました

これは、Quick Startを使用して、nUnitの使用方法を学習しています。私は、コードをコピーしていたし、エラーは、この属性に思い付いた:

[ExpectedException(typeof(InsufficientFundsException))] //it is user defined Exception 

エラーは次のとおりです。

型または名前空間名「ExpectedException」が見つかりませんでした (あなたが不足していますディレクティブまたはアセンブリ参照を使用していますか?)

なぜですか?そのような機能が必要な場合は、どうすればいいですか?

+0

表示されるエラーはありますか?エラーがnUnitまたはIDE内に表示されていますか? – Chawin

+0

あなたのコードはInsufficientFundsExceptionではない例外を返すと思います –

答えて

54

NUnit 3.0を使用している場合、エラーはExpectedExceptionAttributehas been removedです。代わりに、Throws Constraintのような構造を使用する必要があります。

たとえば、あなたがリンクされ、チュートリアルは、このテストがあります。

[Test] 
[ExpectedException(typeof(InsufficientFundsException))] 
public void TransferWithInsufficientFunds() 
{ 
    Account source = new Account(); 
    source.Deposit(200m); 

    Account destination = new Account(); 
    destination.Deposit(150m); 

    source.TransferFunds(destination, 300m); 
} 

次のように変更し、NUnitの3.0の下で動作するようにこれを変更するには:

[Test] 
public void TransferWithInsufficientFunds() 
{ 
    Account source = new Account(); 
    source.Deposit(200m); 

    Account destination = new Account(); 
    destination.Deposit(150m); 

    Assert.That(() => source.TransferFunds(destination, 300m), 
       Throws.TypeOf<InsufficientFundsException>()); 
} 
4

あなたはまだ使用したい場合は、これが最近変更された場合は

[TestCase(null, typeof(ArgumentNullException))] 
[TestCase("this is invalid", typeof(ArgumentException))] 
public void SomeMethod_With_Invalid_Argument(string arg, Type expectedException) 
{ 
    Assert.Throws(expectedException,() => SomeMethod(arg)); 
} 
11

わからないが、NUnitのは3.4.0とそれはを提供します、これを考慮して属性。

[Test] 
public void TransferWithInsufficientFunds() { 
    Account source = new Account(); 
    source.Deposit(200m); 

    Account destination = new Account(); 
    destination.Deposit(150m); 

    Assert.Throws<InsufficientFundsException>(() => source.TransferFunds(destination, 300m)); 
} 
関連する問題