無論出於什麼原因,我似乎無法獲得正確的語法。 你如何進行以下測試工作。我只是有一個簡單的方法TestThrowexception,並希望它通過 我在這裏做錯了什麼?無法使nunit測試異常工作。使用.net 2.0
[TestFixture]
public class ExceptionsTests
{
[Test]
public void When_calling_my_method_it_should_throw_an_exception()
{
//THIS DOES NOT WORK
Person person=new Person();
PersonException ex = Assert.Throws<PersonException>(delegate { person.ThrowPersonException(); },
Has.Property("Message").EqualTo("Test person Exception throw"));
}
}
public class Person
{
public void ThrowException()
{
throw new Exception("Test Exception thrown");
}
public void ThrowPersonException()
{
throw new CustomerException("Test person Exception thrown");
}
public void ThrowArgumentException(string myParam)
{
throw new ArgumentException("Argument Exception", myParam);
}
}
[Serializable]
public class PersonException : Exception
{
public PersonException()
{
}
public PersonException(string message)
: base(message)
{
}
public PersonException(string message, Exception inner)
: base(message, inner)
{
}
protected PersonException(
SerializationInfo info,
StreamingContext context)
: base(info, context)
{
}
}
}
非常感謝這個例子。它是我嘗試過的另一個例子的複製和粘貼。現在所有的作品。謝謝 – user9969