Last time I showed a simple way of using application settings in our wpf application. This time I would like to present you my way of testing Properties.Settings, or rather mocking it in our application tests.
First of all we need to make our application code independent of the concrete implementation of auto generated Settings class. Let’s define an interface which will contains the same properties that are defined in our Settings.
We have one user scope setting and one application scope setting. Their names must match the ones defined in our application settings. Additionally we have two methods. One for reloading settings and one for saving changed settings to config file. Both methods are implemented in ApplicationSettingsBase.
Now since the Settings class is auto generated, we cannot make it implementing our settings interface.
But fortunately it is also partial which gives us a possibility to add a new class with the same name and make any modification on that class
Important note about partial classes: they must have the same name and be placed in the same assembly and same namespace. Having that in place we can now make our application code use the ISettingsService instead of concrete Settings class.
There is only one inconvenience with this approach. Whenever you change, add or delete setting you will need to update the ISettingsService interface.
Now it is quite easy to mock our settings. I use Moq which makes it even simpler here.