Comparing Resource Dictionaries in Unit Tests

Working on the ÜberProf has presented some interesting scenarios. As you might have picked up from Ayende’s blog, the vast majority of the source is shared between all the products. This is especially true in the UI layer.

Now, Ayende wanted to have a distinct color scheme for each application. That by itself wasn’t a big deal, switching out resource dictionary is well documented and very straight forward. The architecture we choose resulted in a resource dictionary for each application with a set of identical keys. Additionally, and because of our design choices, we have to reference everything using DynamicResource. I was concerned about the possibility of omitting a key from one of the dictionaries and consequently breaking the UI. (I know that some people feel the UI is already broken. Don’t be a hater. It will get better.)

I decided to write a test to compare the dictionaries and check for omitted keys. I designated the NHProf dictionary as the “master”. It is the standard that the others are compared against.

Here’s what one of the actual test looks like:

public class EnsureColorsAndBrushesAreConsistentFor
    public void LinqToSql()
        var master = GetMasterListOfKeys();

        var linq2sql = new Client.Resources.LinqToSql.ColorsAndBrushes();

        var missing = from resource in master
                      where !linq2sql.Contains(resource)
                      select resource;

            missing.Count() == 0,
            MissingMessage("LinqToSql", missing));

    private static IEnumerable<object> keys;

    private static IEnumerable<object> GetMasterListOfKeys()
        if (keys == null)
            var master = new ColorsAndBrushes();
            keys = master.Keys.Cast<object>();
        return keys;

    private static string MissingMessage(string dictionaryName, IEnumerable<object> missing)
        return missing.Aggregate(
            string.Format("ColorsAndBrushes.xaml for {0} is missing the following: ", dictionaryName),
            (seed, key) => seed + "\n" + key);

In order for this to work, I had to turn each of the resource dictionaries into a class. This is very easy to do, you only need to add the x:Class attribute to the root.

<ResourceDictionary x:Class="HibernatingRhinos.Profiler.Client.Resources.LinqToSql.ColorsAndBrushes"


There are similar tests for each of the applications.

Posted 12-22-2009 11:37 AM by Christopher Bennage
Filed under: , , ,


About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)