|Number of votes:||2|
I’ve started to study for the certification test and since there are quite a few areas in CMS 6 I haven’t used I started with one of those (hey, everything beats reading up on mirroring right?) namely the dynamic data store.
Also, if you’ve fallen asleep before reaching the end of the post I’ll take this opportunity to wish you a merry Christmas!
When it comes to DDS one thing I quite soon found myself doing was checking if a store existed and create it if not. This seemed like a good opportunity to showcase both how to use StructureMap as well as some testing. Basically this is the same blog post I always write but with new fancy classes!
I’ve chosen to create a store from a type so the first thing to create is an interface that specifies which type we want to create a store for.
My dynamic data class implements it (as well as IDynamicData).
And that’s all I need to do.
The basic flow for the code that actually performs the creating of stores is the following:
Find all classes that implements ISpecifyTypeForDynamicDataStore
Get the type from those classes and create a list
Send the list to some class that checks if the store already exist for that type. If it does not, create it.
An operations like this is quite simple using StructureMap. We simply specify that we want to register all implementations of the interface (line 10) in question and StructureMap will add all to it’s container.
I’ve also created a registry so StructureMap knows which concrete implementation we want for the abstract class DynamicDataStoreFactory.
The actual creating of stores is handled by a class that’s responsible for creating the store. This basically boils down to enumerating through a list of types and if the type does not have a connected store it should create one.
In the startup of our application (in my case, Application_Start in global.asax) I first bootstrap my container and then ask it for an implementation of CreateDynamicDataStores. This class takes two parameters in it’s constructor. The first is a class that implements DynamicDataStoreFactory which StructureMap will resolve to use DynamicDataStoreFactory.Instance. The second parameter is a list of classes implementing ISpecifyTypeForDynamicDataStore. Since we added all types that implements that interface StructureMap is smart enough to send all implementations to the method. For more info on StructureMap check out my introduction.
Like my boulder climbing friend Niklas has blogged about testing code that interacts with DDS is a breath of fresh air. For more info about what Moq is or a more general description of working with unit test s(in a EPiServer centered direction) please read my previous blog post.
For my class CreateDynamicDataStores I’ve created the following specifications:
These specification are written in MSpec (read this post by Joel for more info on MSpec) and looks like this:
For the case were “when a type that does not have a data store is sent to create dynamic data stores” we want to make sure that if the store does not exist for a specific type it’s created for that same type. What this means in terms of the DynamicDataStoreFactory is that it should not return anything (eg null) when loaded with the type and then we need to verify that the method Create is called with the type as a parameter.
First we establish our context (sort of the Arrange)
Then we perform an action (the Act), in this case running the method CreateStoreIfItDoesNotExist
Now we need to check our mocks to verify the expected behaviors.
It should have tried to load the store for the first type in the array
Since this should return null is should also try to create the store for the very same type
(Tests for the other scenario are more or less exactly the same. Only difference is that the GetStore should return a store and we need to verify that the CreateStore is not called).
Still awake? If so, I want to wish you an awesome Christmas!