Monday, August 27, 2018

Nsubstitute basic test & TDD note

I have been working on creating unit tests and to some extent also doing some TDD. I think TDD at the beginning when you come from a background of classical development of develop-first test-later, is a change that might not come so naturally the first time you tried. However the more you try to understand the test driven model the more you actually realize how you can benefit from thinking on what is the purpouse of you code and the how to fix it. Refactoring is a big part of TDD and something I think I am used to but never thought of it like a natural process to deliver a more robust design based on testing while coding. So here is a small example on nsubstitute:


Here we have the beginning of the class to test a cube iterator.  In the image below I created a class to fill the values on a setup mode to be called when the data is about to be treated([TestInitialize]).

The method Setup is where I am going to setup the mocking for my classes.









Finally I test my values on a Given-When-Should pattern so I know what I am passing what method/action I am invoking and what I expect as a result. Nsubstitute handles some easy  fluent methods.