Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 475
TenaciousG Avatar asked Jul 31 '26 20:07

TenaciousG


2 Answers

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:

  1. Reference the MonoTouch assemblies, even in your .Net 3.5/Mono Test Projects
  2. 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.

like image 147
Ben Phegan Avatar answered Aug 03 '26 08:08

Ben Phegan


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.

like image 20
poupou Avatar answered Aug 03 '26 08:08

poupou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!