Question

I wish to use Assert.Throws with a property get.

E.g.

object shouldFail = myobj[fakeField];

my attempts, albeit bad ones to help demonstrate my need.

Assert.Throws<MyException>(() => object shouldFail = myobj[fakeField]);
Assert.Throws<MyException>(() => myobj[fakeField]);

The syntax error I get is Only assignment, call, decrement, and new object expressions can be used as a statement. I understand that. So I am asking without a try/catch, how can I test for an exception on that line of code in the e.g. above?

I am aware of [ExpectedException(typeof(MyException))]

Était-ce utile?

La solution

You can't really do it in lambda with expression since you need to eat return value. Try use statement block instead:

Assert.Throws<MyException>(() => { object shouldFail = myobj[fakeField];});

Note: it is not recommended to throw from get...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top