Windows Live Writer – Debugging and Unit Testing

Over the last few posts I have been talking quite high level, not requiring any debugging or unit testing however in a real plugin these things would be required during development. In this post, I will talk about how to debug a plugin and also how to use MbUnit to create unit tests for your plugin.

For this post, I’m going to use the plugin I created in my previous post which saved information into the global plugin option properties.

As the solution is a class library, we can’t simply hit F5 and jump straight into the debugger.  All the interacts with the plugin are done via LiveWriter so you need some way to access the process and the plugin.

On MSDN, the article which quickly outlines how to create a plugin has at the bottom “Build your plugin and then run Windows Live Writer to test and debug.”

So let’s give it a go.  In the Initalize method of the plugin, set a breakpoint in VS or use the Debugger.Break() command.

public override void Initialize(IProperties pluginOptions)
{
            System.Diagnostics.Debugger.Break();
            options = pluginOptions;
            base.Initialize(pluginOptions);
}
 

Then, go into the properties of the project > debug > Start Action > Start External Program > Set it to “C:Program FilesWindows Live WriterWindowsLiveWriter.exe”.

Hit F5 and Live Writer will launch.  Select to insert the Options plugin (or the one you are trying to debug) and the Visual Studio debugger will kick in.  Great!! You can now easily debug your plugin like a normal application.

So we can now debug, but how about unit testing?  Well, it’s just a standard C# class library so we can create a Plugin.Tests library, reference MbUnit.Framework.dll and your Plugin.dll and you are away!

Your test could then look something like this.

[TestFixture]
public class Tests
{
    [Test]
    public void ContentCreatedAsExpected()
    {
        OptionsPlugin op = new OptionsPlugin();
        op.Initialize(new PropertiesStub());
        string actualContent = string.Empty;
        DialogResult actualResult = op.CreateContent(null, ref actualContent);
        Assert.AreEqual(actualResult, DialogResult.OK);
        Assert.AreEqual(actualContent, “Option not set”);
    }
}

You may notice, I’m using a PropertiesStub object to replace the IProperties parameter.  We need some implementation in place for our plugin code to work. So what I did was just create a simple stub to save and return values.

public class PropertiesStub : IProperties
{
    Dictionary h = new Dictionary();
    public string GetString(string name, string defaultValue)
    {
        if (h.ContainsKey(name))
            return h[name];
        else
            return defaultValue;
    }

    public void SetString(string name, string value)
    {
        if (h.ContainsKey(name))
            h[name] = value;
        else
            h.Add(name, value);
    }

….

}

You would have to follow all the general advice for TDD (like separate logic and UI) in order to have a good test base. Hopefully, you can see that it is easy to both debug and unit test.

Download Unit Test Sample – LiveWriterOptions.Tests.zip

Technorati tags:

Leave a Reply

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