views:

56

answers:

3

Hi, I'd like to Assert that an exception is being thrown and then check some of the properties of the thrown exception.

I was under the impression that I could do something like the following:

ICommand command = CreateCommandObj();
Assert.That( () => command.DoWork(), Throws.TypeOf<ArgumentException>(),                        
                     Has.Property("ParamName").EqualTo("myParam") &
Has.Property("Message").EqualTo("myMessage") );

However this doesn't even compile and looking at the expected parameters for Assert.That I can't see how I would be able to do this? I'm sure I have used this before though...

Note the above is a contrived example to illustrate the point, ignore the fact I am looking for an ArgumentException on a method that doesnt except any parameters :)

Any help appreciated.

1) Cannot convert lambda expression to type 'object' because it is not a delegate type.

A: 

Try && - logical and versus bitwise and

Edit:

I think you're looking for Assert.Throws if you're using the constraint interface. Not sure how you would assert on message. I think the old style is clearer for what you're trying to accomplish:

  try
  {
    command.DoWork();
    Assert.Fail("ArgumentException Expected");
  }
  catch (ArgumentException e)
  {
    Assert.AreEqual("Expected Message", e.Message);
  }
Rob
Tried it but same compilation problems. I think I am mixing something up because I can't seen an Assert.That method overload that will allow me to do what I want.Adding compilation error messages to question.
Ben Cawley
+3  A: 

Ok, sorted.

I need to use the following syntax:

ICommand command = CreateCommandObj();
Assert.That( () => command.DoWork(), 
                   Throws.TypeOf<ArgumentException>()
                   .And.Message.Equals("MyMessage"));

This approach allows me to check properties on the thrown exception. I can add any number of And or Or's to the Assert.

Thanks everyone for the suggestions.

Ben Cawley
+1  A: 

You test for exceptions with the [ExpectedException] attribute on your method. e.g.

[Test] 
[ExpectedException(typeof(InvalidOperationException))]
public void Test_SomeMethod()
{
    something.SomeMehthod();
}

You can set the properties of the expected exception in the constructor of the ExpectedException attribute.

EDIT You could also specify your test attributes like the following:

[Test]
[ExpectedException(ExpectedException = typeof(InvalidOperationException), ExpectedMessage = "Somethings not right")]
public void Test_SomeMethond()
{
    something.SomeMehthod();
}
mdresser
Isn't ExpectedException an obsolete method of asserting for exceptions?I thought this had been superceded by either Assert.Throws<> or using the Constraint syntax?
Ben Cawley
Possibly, this is just the way i've always done it.
mdresser
I guess ultimately it should be down to which ever method is best to maintain readability and consistency with the tests.I suppose it could be argued your suggested way is better due to less lines of code. And it is certainly readable and easy to understand!
Ben Cawley