This blog post consists of three parts
- On generating sample data in general
- Extending AutoFixture with automatic collections generation support
- Adding a sample data generator in Silverlight for Visual Studio/Blend screen previews
[Note: Windows Live Writer did a horrible job in transferring the post to the blog. I've fixed quite a bit on the formatting, but it's still in poor quality. Apologies for that. Let me know if you want the code samples without the formatting]
Generating sample data in general
Creating objects with sample data is a challenge developers often need to handle. The most common use case is for testing purposes, where you need an object with various values set. Some might be important, but many just need to be set to anything (Also called an anonymous variable).
I’ve seen and used various solutions for this. Often you see it either done manually, by an object mother, with test data builders, or by loading existing objects from a database. Many people start out writing a simple reflection tool to handle it, but stops a few hours down the road once the actual complexity involved becomes apparent.
I had a new use case for it this time, and wanted to avoid many of the problems with the solutions above. After searching for and trying various reflection based tools, I found AutoFixture, and was impressed from the get-go. It pretty much does exactly what you’d expect, and has a clean interface as well. Some examples:
- Creating an anonymous string
Result:- anonymousText: aa48c714-6c6a-4ac4-9c27-3658c9e78d5f
- Creating an anonymous object
- Name: Nameb7c2a9fc-a5b0-4836-83d9-be1b057e0ff1
- Age: 1
Take a look a the AutoFixture cheat sheat for a number of good examples. There’s many things you can do to customize the behavior and output.
That covers the introduction to AutoFixture. If you need a sample data creator, for test data or other purposes, I advice you to check it out. Over to the next point.
Extending AutoFixture with automatic collections generation support
There was one feature I was missing in AutoFixture that I really wanted. If an object has a collection (, list, or any other sort of .NET collection) of something, that collection will only be initialized to an empty collection automatically. If I do a change to the Person object and rerun, the PhoneNumbers list below will be an empty list of strings.
AutoFixture have functionality to handle this. If you run the line below first..
..then the output of PhoneNumbers will be something similar to:
However, I would like this to be handled automatically. Doing it the AutoFixture-way you’ll need to know the internal structure of a class to define that it should handle a certain collection. Another problem is collection interfaces. If PhoneNumbers was defined as IList<string>, I would be forced to define it since it causes a runtime exception of:
“AutoFixture was unable to create an instance from System.Collections.Generic.IList`1[System.String], most likely because it has no public constructor.”
So I set out to see if I could extend AutoFixture to handle collections automatically. Seemed like a fun enough way of getting to know AutoFixture better as well as some good reflection fun. The requirements I set was to handle generic collections and interfaces to collections automatically.
First how to use it (Only the second line is new):
The new CollectionsGenerator:
And an accompanying ReflectionHelper:
Note that this isn’t release quality. There’s a few issues, like with recursion if a complex type contains an instance of itself, and the way .NET collections are identified.
Adding a sample data generator in Silverlight for Visual Studio/Blend screen previewsThe actual use case I was trying to solve was creating automatic sample data for XAML pages in Silverlight. This would enable me to better see how pages looked in Visual Studio and Blend, and a way of testing data bindings as well.
Note that AutoFixture doesn’t support Silverlight just yet, but there is a fork available that supports it (If you want to use a newer version, you only need to do a couple of changes to trunk to make it work).
I wanted a more natural interface for the sample data, so I created a SampleDataGenerator class:
At the bottom of the code above I have included use of the CollectionsGenerator class. I also changed the default behavior of StringGenerator, so it only outputs the property name instead of property name + guid.
You can then use it in a sample object that inherits the ViewModel:
And then in the EditPersonView.xaml, include:
That’s all you need to get sample data visible in a XAML viewer. Good way of checking that the interface looks OK and that the binding is set up correctly.
Vote here to get similar support in the actual AutoFixture product.