Search code examples
unit-testingmspec.net-micro-framework

Is it possible to use MSpec to test .net micro-framework projects?


I'd like to be able to use MSpec or another BDD/TDD framework to test code targeted at the .net micro framework. Unfortunately referencing a MF (micro-framework) assembly isn't possible from a non-MF assembly. It's a shame because it's just C# code and unit tests would be really useful in this scenario. Has anyone worked out a way to do unit testing (preferably with MSpec) for .net micro-framework projects?


Solution

  • I downloaded the latest Machine.Specifications and moved all the code into a Micro Framework Class Library project. I got only 323 compile errors. The short list of things that would need to change...

    1. No extension methods
    2. No generics
    3. No LINQ
    4. No custom attributes
    5. No expression trees

    Is it worth trying to rebuild MSpec on this .NET Framework family? Probably not. Is it something the authors/committers will want to keep up with? Probably not.


    I tried customizing the project type GUID and CSharp import based on this goofy forum thread and ended up with a bunch of errors. Including the "unsupported by the compiler error".

    Error 4 TestableMicroLibrary.Tests D:\TestableMicroLibrary\TestableMicroLibrary.Tests\MMP 0x81010009


    I think you're going to have to roll your own. Use conventions and reflection (about the only thing remaining) for the test classes/methods. You can continue to use a behavior driven style, like test case class per fixture.

    public class When_doing_something_neat
    {
        public void It_should_frob_the_widget() 
        {
            Assert.IsTrue(_widget.IsFrobbed);
        }
    }
    

    And rebuild the whole Assert/Should library (remember, no extension methods!).

    public static class Assert
    {
        public static void IsTrue(bool thing)
        {
            if(!thing) throw new AssertionException("It is not true");
        }
    }