11
votes

I am attempting to unit test a Silverlight 3 project. I am using:

When I write a test that does not use Moq, it works as it should.

When I use Moq outside of a test, Moq works as it should. (I mocked a interface and did a verify in a button handler as a proof.)

But when I run a unit test that uses Moq, I always get this:

System.IO.FileNotFoundException: Could not load file or assembly 'System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system cannot find the file specified.
at Moq.ExpressionExtensions.ToStringFixVisitor..ctor(Expression expression)
at Moq.Interceptor.AddCall(IProxyCall call, SetupKind kind) in c:\Build\Moq Drop\moq\WorkingDirectory\trunk\Source\Interceptor.cs: line 104
at Moq.Mock.<>c__DisplayClassc`2.<Setup>b__b() in c:\Build\Moq Drop\moq\WorkingDirectory\trunk\Source\Mock.cs: line 387
at Moq.PexProtector.Invoke<T>(Func`1 function) in c:\Build\Moq Drop\moq\WorkingDirectory\trunk\Source\PexProtector.cs: line 17
at Moq.Mock.Setup<T1,TResult>(Mock mock, Expression`1 expression) in c:\Build\Moq Drop\moq\WorkingDirectory\trunk\Source\Mock.cs: line 371
at Moq.Mock`1.Setup<TResult>(Expression`1 expression) in c:\Build\Moq Drop\moq\WorkingDirectory\trunk\Source\Mock.Generic.cs: line 194
at SilverlightMoq.Test1.TestFirst() in Test1.cs: line 23 

How can it be fixed?


I rebuilt both assemblies for SL 3 with the same results.

I was able to get the test running on the Microsoft Silverlight Unit Test Framework.

http://code.msdn.microsoft.com/silverlightut/

This is the in-browser test framework that seems to be the standard way to unit test SL. The problems are:

  1. you must run all your tests at once

  2. it is brutally slow.

    (~5 times slower than the same tests running in the nunit test framework)

Does anyone know of any other SL test frameworks or a better way to run the tests against this framework?

When I attempt to use TestDriven.Net 's vs plugin I get an error:

System.IO.FileNotFoundException: Could not load file or assembly 'System,...

5

5 Answers

9
votes

Thanks to the information provided by Lee, and the link he provided (http://weblogs.asp.net/nunitaddin/archive/2008/05/01/silverlight-nunit-projects.aspx) I was able to get my Silverlight NUnit tests running in Hudson with code coverage! Awesome stuff hey! And they also run with ReSharper, so I don't feel like I'm being punished any more (with a 20 second delay) for doing TDD in Silverlight.

So, what did I do exactly?

  • I made a Silverlight Unit Test Framework project for SL3:

    (http://www.jeff.wilcox.name/2010/05/sl3-utf-bits/)

  • Then I changed all the test project's Silverlight assemblies' references (except 'mscorlib') to 'Copy Local: True' as detailed in:

    (http://weblogs.asp.net/nunitaddin/archive/2008/05/01/silverlight-nunit-projects.aspx)

  • Then I added references to the Silverlight NUnit 2.5.1 framework provided in the following blog (there are other versions provided by Jeff Willcox in the blog I mention below and Jamie Cansdale in his blog above, but this was the latest version I found):

    (http://wesmcclure.tumblr.com/post/152727000)

  • That's it! After that I could write my tests, run them in ReSharper, and from the NUnit test runner! Plus I could use ncover to get coverage reports!!!

  • EDIT: Oh yes, and if you run any tests that require the UI thread you will get a cross thread error in ReSharper. This is solved by executing those tests within a Deployment.Current.Dispatcher.BeginInvoke(...) call.

  • After using unit for my tests I couldn't run run them from the SL Test Framework web interface, but that wasn't too much of an issue for me. If you do need to get this working then have a look at:

    (http://www.jeff.wilcox.name/2009/01/nunit-and-silverlight/)

  • PS: I also created a NUnit project for the NUnit console runner to run my tests from the NUnit console (Don't try include your non SL test assemblies in the same NUnit project, it won't work because it uses a different nunit.framework library).

I also tried StatLight (http://statlight.codeplex.com/) to run the tests from the console, and that worked well for me, but I am a ReSharper and NUnit fan, so that wasn't the best option for me. There was also a weird bug with StatLight where it wasn't loading my test project resources properly which resulted in some test failures.

I hope this helps somebody. I have not come up with anything new here, but rather just consolidated all the information I wish somebody had summarised for me up front. When I get some time I will blog about this in terms of creating a Habanero framework (http://www.habanerolabs.com) project with SilverLight.

Whew. That's all folks!

5
votes

The answer at the moment seems to be Jamie Cansdale's Silverlight Nunit Project Template:

http://weblogs.asp.net/nunitaddin/archive/2008/05/01/silverlight-nunit-projects.aspx

This template is fantastic and exactly what I was looking for. It works with Resharper too! I hope this saves someone else a few hours.

4
votes

I built a tool for use with continuous integration and to speed up general Silverlight TDD...

http://www.StatLight.net

It now has full support for any version of NUnit compiled to run under Silverlight.

1
votes

What worked for me is making sure the 'Copy Local' property (in the IDE property grid (F4) of the reference System) is set to true.

0
votes

Sounds like a Silverlight 3 issue. Can you get the sources of Moq.Silverlight and NUnitSilverlight and build them on silverlight 3 binaries? It seems these are built with SL 2.