Showing posts with label unit test. Show all posts
Showing posts with label unit test. Show all posts

Friday, September 16, 2011

Resharper Shadow Copy

When running your unit tests with ReSharper, apparently your assemblies all get copied to a temporary directory, where your project gets build and in which the actual tests are run. This allows you to start a second build of a project, while your tests are still running. Handy as this might be, it does give problems sometimes. If, for instance, some of your tests rely on additional files to be present in your output directory (like resource files), they will not be found, since they are not being copied to the temp directory in which the ReSharper tests run.

Only option is, to turn off the shadow-copy capability in the ReSharper Options window. Now your tests will be run in your actual output directory.



I ran into this issue with classes that utilize Assembly.GetExecutingAssembly to get the full file path to a resource. While my test cases ran fine while using the MSpec runner (which uses a rake script which copies all necessary files), they failed using the NUnit runner utilized by ReSharper. Turning of the shadow-copy option resolved the problem.

Friday, April 29, 2011

Unit Test Windows Phone 7 Project

In the windows phone 7 course I recently followed, there was an issue in unit testing a windows phone 7 project (you can find my recent post on this course here). The fact is, you can't create a Visual Studio unit test project for your phone project. Does this mean you cannot unit test your project? Of course it doesn't! Here's a solution to this problem.

First of all, I am using the MVVM pattern in my windows phone 7 project. This means my views, viewmodels and models are separated from each other, so I can place them in different projects. I created a normal windows phone 7 project for my views and a different windows phone 7 class library project for my viewmodels and models. Next thing you can now do is create a third project, also of the windows phone class library kind, which will contain your unit tests. You can see this basic setup in the following picture.

The fact that you take a windows phone 7 class library project for your unit tests is important. This solution will not work if you choose to use for instance a Silverlight class library project.
To this test project I now added the NUnit and Rhinomock dll's using Nuget and with this I can test all I want. One thing I still encountered was an error in my Unit Tests telling me the System.Windows dll could not be found.

This is quite a weird error, especially since the System.Windows.dll is present as reference in the test project. I tried adding different versions of this dll (all in my C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone folder), but no luck. Eventually I found that the dll was present in the project, but it just didn't get copied to the bin/Debug folder. Changing its Copy Local setting to True did the trick (and I kicked myself for losing quite some time on this stupid error).
So, there you go, a quick and easy way to unit test windows phone 7 projects. Soon I hope to give you some posts about my first Silverlight game and after that my first XNA game.