How to use Moq and nUnit to write Unit Tests for Windows Runtime libraries

I love to use Moq library, it is the best mocking framework for me. Visual Studio provides Fakes framework, but my opinion is that it is for slightly different issues. Fakes is a framework for making fakes, not mokes. For example if you need to change how System.IO.File.Exist handles the path or if for some reason you need to change return value of System.DateTime.Now to test something related to time – this is good job for Fakes framework. If you need to make a Stubs or Mocks I prefer to use Moq, it is just simpler. And don't forget that they work together really well.

The problem which I met when I started to write gMusic is that Moq does not exist for Windows Store Apps (WinRT). It is because "winrt doesn't allow dynamic codegen". But I found a workaround. Instead of creating new Unit Test Class Library for Windows Store Apps I created simple Class Library with Target Framework 4.5 (I will call it Unit Test library). After this I added reference to Windows Runtime using this blog post Referencing the Windows Runtime from Desktop Apps. And the latest point is to include my code from Windows Store Apps library to this Unit Test library. I cannot reference it, because my libraries have different runtimes: Windows Store Apps library has runtime .NETCore (or WinRT, or Windows Runtime, so many names for this). And my Unit Test library has runtime .NETFramework 4.5. Visual Studio does not allow you to mix runtimes, except if they are Portable Libraries. The solution for this is to just include all my code which I want to test from Windows Store Apps library to Unit Test library as links to files Adding an Existing Item as a Link (you can just use drag'n'drop in Solution Explorer with pressed Shift+Ctrl to move some folder or files from one project to another and make it as links).

It is not a perfect solution, but works for me.

How to improve performance of your own Inversion of Control Framework?

I enjoy doing anything by myself instead of using well know frameworks. I am not saying that I am using only my own frameworks. It just gives me more knowledge about how these frameworks doing their job. Inversion of Control Framework it is one of these examples. I have a blog post on Russian about simple implementation of IoC container. You can look on it here Simple IoC Container (I used Microsoft Translator for this link). Actually right now is more than just one class, I have my own framework, which I use for some my applications, like gMusic (you can follow this project on GitHub – Framework).

Couple months ago I met an article IoC Container Benchmark - Performance comparison, where this guy compare different IoC containers (when I first time met this article it was only about performance). So I decided to check performance of my simple container. This is result (I have different count of iterations, so do not compare it to the original blog post):

In this list: "Outcold" – this is my current framework, "IoC A" – it is a class which I showed in this blog post Simple IoC Container (based on Activator.CreateInstance), "IoC E" – it is a different version of IoC class, but based on Expressions (I showed in in one of my comments here). "LightInject" – it is one of the faster frameworks from original blog post about IoC performance, "Unity" – it is one of the famous frameworks. As you can see that, the version of IoC class which is based on Expressions has the worst performance. The funny thing, that I used it everywhere, I really thought that it should be faster than Activator, somebody told me… and I did not verify that…. Anyway, after this article, I spent some time to improve performance of my IoC container, and this is how I did this.

The main problem of IoC container which I implemented is that I did not cache the compiled Expression. Each time when I ask IoC to resolve some object – it compiles expression and after this resolve it:

 

First optimization is simple. Just need to cache expression after first time somebody will ask to resolve object. However, first I need to change how I invoke constructor, because my expression expects constraints I need to change it to parameters, so each "resolving" can use it is own parameters:

 

Now I have Delegate, which can create for me a new object of specific type. The only one problem, that this delegate can have different set of parameters, so I cannot cast it to something like Func<Type1, … , TypeN, Result> and invoke it. I need to call DynamicInvoke, which is slow:

 

This is the result of this optimization (as you can see it is very good, but still variant with Activator works better):

 

The second step I made – I just changed Expression in such way, so it always takes just one parameter "array of objects". In expression I use this array to cast each item of this array to the parameter in constructor. This is how I do this:

 

This is the result:

 

It is faster than version with Activator! But it still slower than LightInject. I guess it is because LightInject used before System.Reflection.Emit instead of Expressions, or maybe they have better idea how to create this expression for type resolving.

Anyway, it was a good result for me.

Meet IntelliCommand (Visual Studio 2010/2012 extension)

How many shortcut keys you know in Visual Studio? Do you want to know all of them? I know how you can learn them very easy.

I'd like to introduce you a cool extension for Visual Studio 2010/2012 which I wrote with help of my colleagues Drake Campbell and Aditya Mandaleeka. Let me just copy-paste description from Visual Studio Gallery:

IntelliCommand - an extension for Visual Studio 2010 and 2012 which helps to find the short keys. It shows the help windows with all possible combinations when you press Ctrl or Shift or Alt or their combinations (hold it for about 2 seconds to see this window). Also it shows the list of possible combination when you press first combination of chord shortcut keys, like Ctrl+K, Ctrl+C (this combination comments selected text in editor).

Moving site to the Windows Azure Web Sites

It was a year and half when last time I updated my web site. Sadly this is just one project I have where I can have some experience with web development. And because I really missed it, so I've put three things on my agenda:

  • Move domain from ru to com, so now my site is available on http://outcoldman.com.
  • Add support for mobile devices and make some minor changes.
  • Move site from masterhost to orcsweb. I have both of them for free, but second one gives better conditions, like more storage and better network bandwidth. And I like orcsweb, because these guys update their servers on next days after new .NET Framework ships.

But how you can understand from the topic I failed last task. Because orcsweb sets medium trust for shared sites – this is a huge pain for developers. For example I met problems with using AntiXSS.

Object Release Verification Framework

Computer Software can have bugs and some of these bugs are Memory Leaks. It is usual case that if your software is a Multi-Document application you need to pay more attention on memory leaks. The main question in these applications is how I can be sure that when user will close document I will release all instances and resources related to this document? What if you already found a Memory Leak and fixed it? How you can protect yourself to be sure that after some changes you will not have this memory leak again?

I had the same thought after one of my Memory Leak fix. Do we have any software or tools which can validate that after some interaction with my application all instances are released, which I want to be released? In native world there are should be a lot of different instruments / practices to do this (I'm not an expert, but pretty sure that this is true). In managed world there are a lot of tools with names Memory Profiler, but to make them work you need to run them separately, analyze your application and after that analyze the report to find memory leaks. This can work, but I'd like to have some instrument in my application which can in runtime check my application and find memory leaks. The solution which I found is pretty simple. I store the list of WeakReferences to objects which I want to check after some interaction that they are released. Based on this I wrote Object Release Verification Framework with couple classes and interfaces. This framework is published on nuget.org as a portable library.

Let's take a look on example.

1  2  3  4  5  6