I have some unit tests where I’m using the DoesNotThrow feature of Nunit. Up until recently, I wasn’t concerned w/ the actual response coming back. However, I recently needed to add a check for the count on my result set. I now have a redundant call in my code. Can I clean this up and make use of the delegate within my second Assert call?
Assert.DoesNotThrow(() => new GetMyCollection.GetCollection(request));
var response = new GetMyCollection().GetCollection(request);
Assert.Greater(response.MyCount, 0);
Just get rid of the first line entirely:
var response = new GetMyCollection().GetCollection(request);
Assert.Greater(response.MyCount, 0);
If new GetMyCollection().GetCollection(request)
throws an exception, that will make the test fail anyway.
Assert.DoesNotThrow
is relatively pointless, IMO - I would only usually use it to contrast with some Assert.Throws
calls.
See more on this question at Stackoverflow