Assert.NotNull should yield its argument


Within the Assert phase of a test, Assert.IsType can often lead to a very legible way of expressing the flow of one's observations. In this context, one is often also doing Null checks.
i.e., I want to be able to do an assert and an assign (0r follow-on Assert):

var result = Act();

var links = Assert.NotNull( result.Links);
Assert.NotEmpty( Assert.NotNull( links.Self));
Assert.NotEmpty( Assert.NotNull( links.SeeAlso));

Some reasons it should not be done:
  • encourages overly constraining Assert sections in tests
  • things like Assert.NotEmpty and Assert.IsType already do checks like this
  • this sort of assertion is a bad idea in the Arrange and Act phases
Closed May 5, 2013 at 1:25 AM by BradWilson
The reason IsType and Single do this is because either the type changes in the former, and to save an extra line of code/re-enumeration in the latter. There is no value in this feature as specified.