Over a million developers have joined DZone.

Comparing Resource Dictionaries in Unit Tests

DZone's Guide to

Comparing Resource Dictionaries in Unit Tests

· ·
Free Resource

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.



Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}