One step past Assert.Throws<>

If you’re not using xUnit (and you should be), at least consider adopting the practices they have for dealing with exceptions in unit tests. From the very first release, they moved us from this:

[ExpectedException(typeof(InvalidOperationException))] public void WithdrawingMoreThanBalanceThrows() {     Account account = new Account();     account.Deposit(100);     account.Widthraw(200); }

to the far-better:

public void WithdrawingMoreThanBalanceThrows() {     Account account = new Account();     account.Deposit(100);     Assert.Throws<InvalidOperationException>(delegate                                              {                                                  account.Widthraw(200);                                              });`` }

But we’re still not following the 3 As pattern. Recently (not sure exactly when) they took us one step further to this (in 3.5 syntax):

public void WithdrawingMoreThanBalanceThrows() {     Account account = new Account();     account.Deposit(100);     Exception ex = Record.Exception(() => account.Widthraw(200));     ex.ShouldBeType<InvalidOperationException>();`` }

and now we are exactly where we should be :)

(I was just told that () => is called the ‘crotch operator’ today. Can’t wait to use that in a code review.)