Unit testing Monotouch
Has anyone successfully managed to run unit tests for their Monotouch project? I have read other posts where people are told to manually add references to the appropriate assemblies. Doing this makes the project compile, but it will still not run the tests.
I have a solution with two projects; a Monotouch Navigation project and an NUnit Library Project. I added a reference to my monotouch project and to the monotouch and other needed assemblies to the test project. Tests that only runs code outside the monotouch assembly will run fine, the ones that accesses monotouch code fails with:
System.IO.IOException: Write failure ---> System.Net.Sockets.SocketException: The socket has been shut down at System.Net.Sockets.Socket.Send (System.Byte[] buf, Int32 offset, Int32 size, SocketFlags flags) [0x00000] in :0 at System.Net.Sockets.NetworkStream.Write (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in :0 --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.Write (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in :0 at System.IO.BufferedStream.Flush () [0x00000] in :0 at (wrapper remoting-invoke-with-check) System.IO.BufferedStream:Flush () at System.IO.BufferedStream.Dispose (Boolean disposing) [0x00000] in :0 at System.IO.Stream.Close () [0x00000] in :0 at Mono.Remoting.Channels.Unix.ClientConnection.ProcessMessages () [0x00000] in :0
I found a post saying: "..if you're not running on the iPhone or in the iPhone sim开发者_JAVA百科ulator, there's no way to call the necessary native APIs to instantiate the components.."
So what I´m really wondering is if it´s actually possible to do unit testing in Monotouch at the moment, or if not, what all you other guys do?
Thanks!
You don't actually have to create a second solution, or pull out OS-agnostic code. There are a few tricks to it though. Generally:
- Reference the MonoTouch assemblies, even in your .Net 3.5/Mono Test Projects
- A Supervisor Controller pattern helps with the amount of code you can test.
Have written up a few details on what we have found to be best practice so far. You can find it here: http://ben.phegan.name/index.php/2011/02/28/monotouch-and-unit-testing.
Has anyone successfully managed to run unit tests for their Monotouch project?
Yes this is now possible with the latest MonoTouch 5.1.x (beta) releases.
I found a post saying: "..if you're not running on the iPhone or in the iPhone simulator, there's no way to call the necessary native APIs to instantiate the components.."
That's true. This is why a runner, executing on iOS (simulator or devices), was built to enable the execution of MonoTouch related unit tests.
That was my post you found. So far this is the best I've been able to do:
- Pull the OS-agnostic code out into separate libraries.
- Create a second solution, with its own project files, to compile those libraries as vanilla .NET 3.5.
- Unit-test the libraries.
On the bright side, this encourages separating your concerns and (in theory) should it easier to port the app to, say, MonoDroid. But keeping the two solutions in sync is a pain, and it's easy to drift away from actually writing tests when they're out of sight.
Yes, it is possible.
The Xamarin Docs Team has added a document on using the built-in unit tests template, which operates using a modified version of the NUnitLite project.
精彩评论