Dynamic TDD

This post is about Dynamics in C#, TDD and how we can mix all of this in a fun and fluent way ;-)

I presented that at the last ALT.NET Fr meetup and I want to share it more widely now after the very good first impressions given by the community.

For a long time now, I thought about how dynamic can help writing tests. Since my friend  Thomas Pierrain started the NFluent library, I tried to keep in mind introducing dynamic in it’s library because I think it could help.

I then came with some ideas in a form of a small POC that I want to share with you. Instead of a long discourse, I preferred to record the following small videos because the most interesting part of the concept is how things changes constantly in a very small feedback loop. And thanks to Remco Mulder and it’s continuous testing tool NCrunch, it’s even better and quicker ;-)

Introduction : problems, concepts

Dynamic TDD intro

Demo 1 : 2 step dynamic implementation

Dynamic TDD Demo 1

Demo 2: Function mocking facilities

Dynamic TDD Demo 2

As I said, this is now mostly ideas, concepts, tests but I think it’s important to go deeper in that direction because getting a better feedback loop is very important and we also have to test things like that to discover new usages.

Any ideas, comments are welcome, you can post comments here, ping me on twitter (@rhwy) or send me a mail to rui at rui dot fr.

happy coding!

Posted in Articles, Projects Tagged with: , , ,
2 comments on “Dynamic TDD
  1. As I already said, this is very promising. But since I have no strong experience of dynamics, I think I have to intensively play with it before I make you a relevant feedback.

    Nonetheless, here is the cinematic I would like to be able to achieve as a TDD practitioner:

    1. I create a test method: public void DefaultSeatCountIs5()
    2. I create a dynamic variable: dynamic car = new object();
    3. Then, I’m able to type the line: Check.That(car.SeatCount).IsEqualTo(5); without breaking my continuous ncrunch build, but leading to a failing test/RED (of course, it will use a Check.That(dynamic dynamicSut) overload)
    4. I enable the “switchMyRED” keyboard shortcut (e.g. Ctrl-R-D) while my cursor is still within the test method scope => the NFluent plugin prompts me a popup within my IDE, with a default “Car” type to be created from the dynamic instantiated within the test method (and some checkbox options like: create this new type in a dedicated File)
    5. I push “enter” => the NFluent plugin creates the proper Car skeleton class and replaces the dynamic instance with it within the test (which should still fail with this non-dynamic implementation)
    6. I implement the Car class with enough code to make it green => test is passing
    7. I do the same and add another test: public void CarCanOnlyStartWithFuel() with a dynamic such dynamic car = new object(); but “playing” with other method names/properties (Refuel (), Start(), …)
    8. When I automatically switch the “RED” implementation for this test (from dynamic to non-dynamic type), the NFluent plugin recognize there is already a “Car” class, and asked me whether I want to merge these new methods/properties into the existing “Car” class (default value), or to create a new class with another name.
    9. Same magical trick as before…

    For me, it should be mandatory for NFluent 2.0 to allow such magic to happen (thanks to a new NFluent VS.NET plugin).
    What do you think?

  2. SeriousM says:

    Hi, is there any change to get a sight on the implementation of your ClassFactory? I would love to try it out myself.

    Thank you very much.

Leave a Reply to SeriousM Cancel reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>