c# - Cannot make nunit test exception to work .Using .net 2.0 -
for whatever reason cannot seem syntax right. how make following test work. have simple method testthrowexception , pass doing wrong here?
[testfixture] public class exceptionstests { [test] public void when_calling_my_method_it_should_throw_an_exception() { //this 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) { } } }
apart issues type of exception throwing, this. think more readable.
[test] public void when_calling_my_method_it_should_throw_an_exception() { person person=new person(); personexception ex = assert.throws<personexception>(delegate { person.throwpersonexception(); }); assert.that(ex.message,is.equalto("test person exception thrown"); }
Comments
Post a Comment