简体   繁体   English

在monotouch / monodroid中使用模拟进行单元测试

[英]unit tests with mocks in monotouch/monodroid

I'm currently developing an app on MonoTouch/MonoDroid and I'd like to improve my testing (working on a Mac with xamarin studio). 我目前正在开发MonoTouch / MonoDroid上的应用程序,我想改进我的测试(使用xamarin工作室在Mac上工作)。 I know how to write integration tests which run on the simulator (at least on iOS with the iOS Unit Test Project). 我知道如何编写在模拟器上运行的集成测试(至少在iOS上使用iOS单元测试项目)。

What I'd like to do now is TTD, with small, mocking-only tests which test one designated class (and do not run in any device/simulator - unit-testing only). 我现在要做的是TTD,只有小的,仅模拟测试,测试一个指定的类(并且不在任何设备/模拟器中运行 - 仅限单元测试)。 I use to do that on Java with Mockito and tried to do the same on my mono solution with Moq 4.0. 我用Mockito在Java上做了这个,并尝试在我的Moq 4.0单声道解决方案上做同样的事情。

For that purpose, I need nothing from Mono, just the plain classes. 为此,我不需要Mono,只需要普通的课程。 So I tried to create a new N Unit Project and linked my classes into that project. 所以我尝试创建一个新的N单元项目并将我的类链接到该项目中。 But now, to get the code compiling, i need to include all the references I'm using in that classes (eg Xamarin.Mobile.Geolocator, RestSharp, Geo, etc.), even If I mock them anyway later. 但是现在,为了获得代码编译,我需要包含我在这些类中使用的所有引用(例如Xamarin.Mobile.Geolocator,RestSharp,Geo等),即使我以后仍然嘲笑它们。 When I do that, the code compiles, but when I run the first test, there's an error saying version xy of System.Core is not available. 当我这样做时,代码编译,但是当我运行第一个测试时,有一个错误,说System.Core的版本xy不可用。 The problem makes sense, the libraries use a specific, mono-version of the System-libraries which is not available in that test project. 问题是有道理的,库使用系统库的特定单声道版本,该版本在该测试项目中不可用。

The only solution I see is to avoid these references and to write empty Stubs for all library-classes I use in my code, just to make it compile. 我看到的唯一解决方案是避免这些引用,并为我在代码中使用的所有库类编写空的Stub,只是为了使其编译。 But that's a lot of useless work, which I really try to avoid. 但这是很多无用的工作,我真的试图避免。

So to make a long story short: Can someone help me with a working setup for test-first developing in xamarin mono? 所以简而言之:有人可以帮助我使用xamarin mono进行测试优先开发的工作设置吗? Where I can use a mocking framework like Moq and don't need to run my unit tests on a simulator? 我可以在哪里使用像Moq这样的模拟框架,而不需要在模拟器上运行我的单元测试? Important is also there is some solution to mock third-party libraries (and makes the code compile in my test-project). 重要的是还有一些模拟第三方库的解决方案(并使代码在我的测试项目中编译)。

I haven't yet used the new tools from Xamarin 2 - my licence expired, so I'm still using the old Mono for Android in VS. 我还没有使用Xamarin 2中的新工具 - 我的许可证已过期,所以我仍然在VS中使用旧的Mono for Android。

The test-first approach is a challenge on the platform for sure. 测试优先方法肯定是对平台的挑战。

The best thing to do if you are prepared to separate the mobile-specific libraries with indirections is to create a normal class library project targeting the .NET framework, add the files you want to test and choose the Add As Link option. 最好的事情做,如果愿意的移动专用库与间接性分开是建立一个正常的类库项目面向.NET框架中,添加你要测试的文件,并选择添加链接选项。

You need good separation of domain logic from platform specific logic for this to work of course, but you have access to the full power of the various testing/mocking frameworks. 您需要将域逻辑与特定于平台的逻辑完美地分离才能实现此功能,但您可以充分利用各种测试/模拟框架的功能。

The other option is to separate out the cross-platform elements into a Portable Class Library (PCL) and test the same way. 另一种选择是将跨平台元素分离为可移植类库(PCL)并以相同方式进行测试。 There are some pros and cons with the PCL route - more info in this answer. PCL路线有一些优点和缺点 - 在这个答案中有更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM