Two options:

1. Assign to a variable.

Assert.Throws<FooException>(() => { var x = obj.MyProperty; });

2. Wrap with a dummy method call.

Assert.Throws<FooException>(() => GC.KeepAlive(obj.MyProperty));

Jeff.

On Wed, Jan 13, 2010 at 6:34 AM, Holf <[email protected]> wrote:

> Hi,
>
> I've been using Assert.Throws with the nice Lambda syntax and really
> like it. However, is it possible to test for an exception in a
> property? For example:
>
> // arrange
> Section section = new Section { Name = "DummySection" };
>
> // assert
> var exception = Assert.Throws<PostconditionException>(() =>
> section.CurrentVersion);
>
> Assert.Throws expects to be passed a Method, not a Property so I get
> an 'Only assignment, call, increment, decrement, and new object
> expressions can be used as a statement' error in VS2008.
>
> Yes, I could refactor the code in question into a Method but all I am
> doing is using a DesignByContract library to ensure that another
> property on the same object is not null. I'd rather not unnecessarily
> complicate the code.
>
> Any thoughts much appreciated.
>
> --
> You received this message because you are subscribed to the Google Groups
> "MbUnit.User" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<mbunituser%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/mbunituser?hl=en.
>
>
>
>
--
You received this message because you are subscribed to the Google Groups "MbUnit.User" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/mbunituser?hl=en.

Reply via email to