public static void ShouldNotBeNull<T>(this T actualValue)
{
Assert.IsFalse(actualValue == null, "Object is null, and should not be null.") ;
}
I would like to provide a variable name in the error string on my tests. I have this as an extension method. Is it possible to get the real parameter name of the variable being passed in?
One option I’ve written up before is using anonymous types. For example, you’d have:
with a generic
AssertNotNullmethod which performs some reflection etc once, and caches the property accesses as delegates in order to hit performance as little as possible.It’s not something I’d generally recommend though. It’s not refactor-proof, it creates a new object on each call, and it’s generally a bit of an abuse of anonymous types. Admittedly if this is only for tests, the performance aspect isn’t as bad…