→ nUnit

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.

The art of Unit Testing with Examples in .NET

osherove_coverFirst time when I familiarized with unit testing was 5 or 6 years ago. It was start of my developing career. I remember that somebody told me about code coverage. At that time I didn’t write any Unit tests. Guy, who was my team lead, told me “Do you see operator if with three conditions? You should check all of these conditions”. So, after that I had written some code, I should go to interface and try to invoke all code which I wrote from user interface. Nice? At current time I know little more about tests and unit testing. I have not participated in projects, designed by Test Driven Development (TDD). Basics of my knowledge are a spying code of my colleagues, some articles and screencasts. I had decide that I should know much more, and became a real professional of unit testing, this is why I had start to read book The art of Unit Testing with Examples in .NET. More than, in my current job place looks like I’m just one who writing unit tests for my code. I should show good examples of my tests.

Regular Expressions. Remember it, write it, test it.

I should say that I’m fan of regular expressions. Whenever I see the problem, which I can solve with Regex, I felt a burning desire to do it and going to write new test for new regex. Previously I had installed SharpDevelop Studio just for good regular expression tool in it (Why VS doesn’t have one?). But now I’m a little wiser, and for each Regex I write a separate test.

I find it difficult to remember the syntax of regular expressions (I don’t write them very often); I always forget which character is responsible for the beginning of the line, etc. So I use external small and easy articles like this “Regular expressions - An introduction”.

Now I want to show you little samples of regular expressions and want to show you how to test these samples.