简体   繁体   中英

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). I know how to write integration tests which run on the simulator (at least on iOS with the iOS Unit Test Project).

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). I use to do that on Java with Mockito and tried to do the same on my mono solution with Moq 4.0.

For that purpose, I need nothing from Mono, just the plain classes. So I tried to create a new N Unit Project and linked my classes into that project. 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. 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. 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. 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? Where I can use a mocking framework like Moq and don't need to run my unit tests on a simulator? 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.

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.

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. There are some pros and cons with the PCL route - more info in this answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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