与起订量,Silverlight和NUnit的单元测试 [英] Unit testing with Moq, Silverlight and NUnit

查看:242
本文介绍了与起订量,Silverlight和NUnit的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图单元测试Silverlight 3的项目。我使用的:

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__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 

怎样才可以解决吗?

我重建的SL 3具有相同的结果两个组件。

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

我是能够得到测试在微软的Silverlight单元测试框架运行。

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

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

这是在浏览器的测试框架,这似乎是标准的方式进行单元测试SL。问题是:

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


  1. 您必须立即

  2. 运行所有测试
  3. 这是惨遭缓慢。

  1. you must run all your tests at once
  2. it is brutally slow.

(〜比NUnit测试框架运行相同的测试5次慢)

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

有谁知道任何其他SL测试框架或更好的方式来运行针对这个框架的测试?

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

当我尝试使用TestDriven.Net的VS插件我得到一个错误:

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

System.IO.FileNotFoundException:未能加载文件或程序系统,...

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

推荐答案

由于李提供的信息,链路他提供
http://weblogs.asp.net/nunitaddin/archive/2008/05/01/silverlight-nunit-projects。 ASPX
我能够得到的 NUnit的测试= http://en.wikipedia.org/wiki/Hudson_%28software%29\">Hudson 与代码覆盖!真棒的东西哎!他们还与 ReSharper的跑,所以我不觉得我被惩罚的话(用20秒延迟)用于在Silverlight中做 TDD

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?


  • 我做了一个Silverlight单元测试框架项目SL3:

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

http://www.jeff.wilcox.name/2010/05/sl3-utf -Bits /

然后,我改变了所有的测试项目的Silverlight程序集引用(除了'mscorlib程序),以复制本地:真详见:

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

然后我说在以下博客(有杰夫·威尔科克斯在我之下,杰米Cansdale提及上面他的博客博客提供的其他版本所提供Silverlight的NUnit的2.5.1框架的引用,但这是最新版本我发现):

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/后/ 152727000

这就是它!从那以后,我可以写我的测试,在ReSharper的运行它们,并从NUnit的测试运行!另外,我可以用ncover获取覆盖报告!!!

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!!!

编辑:哦,是的,如果你运行需要UI线程的任何测试,你会得到一个在ReSharper的跨线程错误。这是由 Deployment.Current.Dispatcher.BeginInvoke(...)调用内执行这些测试解决。

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.

使用单位为我的测试我无法运行从SL测试框架的Web界面运行它们,但这不是太大的问题,为我之后。如果你需要得到这个工作,然后看看:

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/

我也试过StatLight( http://statlight.codeplex.com/ ),以从控制台运行测试,而且工作很适合我,但我是一个ReSharper的和NUnit风扇,所以这是不是对我最好的选择。还有与StatLight一个奇怪的错误,这错误是不正确加载我的测试项目资源,这就造成了一些测试失败。

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.

我希望这可以帮助别人。我还没有想出什么新的东西,而只是巩固了所有我希望有人曾总结了我前面的信息。当我得到了一段时间,我会在博客这个在创造一个阿巴内罗框架( http://www.habanerolabs.com )项目,条款SilverLight的。

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.

哇。这是所有乡亲!

这篇关于与起订量,Silverlight和NUnit的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆