Unit testing with Moq, Silverlight and NUnit

百般思念 提交于 2019-12-02 23:58:14
Mark Whitfeld

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!

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.

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!