I’ve often thought shallowly about but never had much chance to explore the idea of a new programming language or enhancements to existing programming languages for unit testing. Unit testing has come a long away over the past 5 years but I still feel like there’s a lot of room for improvement.
I’m a big fan of constructor dependency injection because it makes the depdencies of your class crystal clear. If you add a dependency, you have to review all of the code that instantiates instances of your class, which usually is a very good thing. I frequenty add a check in the constructor that throw a null argument exception if the actual parameter passed in is null. Resharper makes this easier, but I’d really like to see a way to do this declaritively, like field nullability in table declarations in SQL DDL. EG, in SQL you have:
CREATE TABLE Blah (counter int not null)
Wouldn’t it be nice to have something like this in a c# constructor?
public SomeClass(notnull IDataAccess access)
Instead of adding a check for null and throwing an exception yourself (or having resharper do it), the compiler would take care of this for you. Also, you wouldn’t need to test that the constructor throws an exception when a null actual parameter is passed in.
Actually, the more I think about this, the more I’d prefer that constructor arguments default to not being nullable and instead have a nullable key word.
Has anyone thought of any other things that you could do to tweak a language to make it more unit test friendly?