views:

3208

answers:

6

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

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

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__DisplayClassc2.<Setup>b__b() in c:\Build\Moq Drop\moq\WorkingDirectory\trunk\Source\Mock.cs: line 387 at Moq.PexProtector.Invoke<T>(Func1 function) in c:\Build\Moq Drop\moq\WorkingDirectory\trunk\Source\PexProtector.cs: line 17 at Moq.Mock.Setup(Mock mock, Expression1 expression) in c:\Build\Moq Drop\moq\WorkingDirectory\trunk\Source\Mock.cs: line 371 at Moq.Mock1.Setup(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

Anyone have any ideas on this?

A: 

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.

Hadi Eskandari
A: 

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,...

The FileNotFoundException most likely comes from the fact that TestDriven.Net is using the desktop CLR and there's a mismatch in the System.IO library it's trying to load (2.0.5.0 vs 3.0.0.0).
David Cuccia
I ran into the same issue with the silverlight unit testing framework being way too slow so I built my own light weight unit testing framework. You can check it out here http://software.herbrandson.com/index.php/2009/09/faster-silverlight-unit-testing/ . My test suite of 160+ tests was taking over 30 seconds to run with the MS testing framework, but takes just over 1 second with mine. Hopefully it can be useful to others.
herbrandson
+3  A: 

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.

+1  A: 

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.

Jason Jarrett
+1  A: 

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.

A: 

Thanks to the info 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 wont 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 info 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! PS Sorry I had to disable all the hyperlinks because Stack overflow would count this as spam otherwise.

Mark Whitfeld