Reputation: 138
At work I have a function that generically does this:
public SomeObject MethodWithTryCatch()
{
//some logic
try
{
//some logic that may throw an exception
}
catch(Exception exception)
{
return new SomeObject
{
//list of properties
ErrorMessage = exception.Message
}
}
}
I want to test a couple things:
Questions:
ErrorMessage = exception.Message
how can I make sure the expected object and actual object will have the same ErrorMessage
value?Upvotes: 0
Views: 72
Reputation: 5791
Try using Assert.Throws
statement to verify the exception is raised.
eg
[Fact]
public void VerifyCatch()
{
var f = new Foo();
var e = Assert.Throws<SomeException>(() => f.MyMethod());
Assert.Equal("My expected message", e.Message);
}
Upvotes: 1