Los Techies : Blogs about software and anything tech!

Learning how to walk with Mono


I started experimenting with Mono this week. The reason is simple enough: somebody wants me to write a small application that will run on their Windows box. The problem is, I own a Mac and a Linux powered laptop (yes, taking the Vista sticker off was a exercise in patience that left a lot of residual glue on the laptop...) Mono is a perfect solution for writing .Net software on Linux that will run on Windows.

The Setup

I run OpenSuse 11.1 because I figured I might as well go with the distribution that was created by the people who are currently behind Mono: Novell. Also, at the time of this writing, I have Mono 2.4, which is .Net 3.5 compatible, and Monodevelop 2.0 installed. As a bonus, I realized today that NUnit was also installed: version 2.4.8.

The Solution

Creating a new solution with Monodevelop is pretty straightforward.

As you can see, it's fairly similar to the Visual Studio experience. I selected the Library type of project and named it MyTest. After clicking the Forward button, Monodevelop asked me if I wanted to add packaging, unix integration or other little nifties to my project. I graciously declined.

The above image depicts what you get when you create a new, empty library project. Eager to get on my way, I coded one of the simplest class I've ever coded:

   1:   
   2:  using System;
   3:   
   4:  namespace MyTest
   5:  {
   7:      public class MyClass
   8:      {
   9:          public string MyString { get; set; }
  10:          public MyClass()
  11:          {
  12:          }
  13:      }
  14:  }

And I then set out to write a test using Scott Bellware's SpecUnit. First, I added a NUnit library project to my solution, which I called MyTest_spec.

This created a simple, empty project with everything you need to use NUnit and write some tests. The interesting thing I discovered is that Monodevelop includes its own Reflector utility. Going to the references of my NUnit project and double-clicking on nunit.framework, I got this:

Which is how I figured out I was using version 2.4.8. You can drill down into the assemblies and click on methods. I guarantee it; you will see code! Anyway, here's my test code:

   1:   
   2:  using System;
   3:   
   4:  using MyTest;
   5:   
   6:  using NUnit.Framework;
   7:  using SpecUnit;
   8:   
   9:  namespace MyTest_spec
  10:  {    
  11:      [TestFixture()]
  12:      [Concern("MyClass")]
  13:      public class when_changing_the_string_of_my_class : ContextSpecification
  14:      {    
  15:          [Observation]
  16:          public void should_persist_the_new_string()
  17:          {
  18:              myClass.MyString.ShouldEqual("Hello, world!");
  19:          }
  20:          
  21:          protected override void Because ()
  22:          {
  23:              base.Because ();
  24:              myClass.MyString = "Hello, world!";
  25:          }
  26:   
  27:          
  28:          protected override void Context ()
  29:          {
  30:              base.Context ();
  31:              myClass = new MyClass();
  32:          }
  33:          
  34:          protected MyClass myClass;
  35:      }
  36:  }

Please note that Monodevelop default to using the .Net 2.0 framework. You will have to change this to .Net 3.5. Right-click on each one of the projects of your solution, select Options and go to Build/General. Change the runtime version to .Net 3.5, like shown below:

I've got my class, I've got my spec. Now I need to run the tests and see if this whole thing works on Linux! So I compiled... And BAM! I got an error. The compiler complained that SpecUnit was expecting the 2.4.6 version of NUnit. The problem is, as you've seen higher in this post, I was using nunit-2.4.8. I went back online and downloaded the SpecUnit code, thinking I'd recompile it with the 2.4.8 dlls of NUnit. I found the solution file (SpecUnit was created with Visual Studio, so I was looking for a .sln file) double-clicked on it and crossed my fingers... It worked! Monodevelop loaded the entire SpecUnit solution! All I had to do then was to replace the nunit.framework references with the newer ones from the 2.4.8 version, which were conveniently located with all the other regular .Net references. Recompile and voila! A brand new SpecUnit, ready to roll!

To tell you the truth, I wasn't expecting Monodevelop to load a .sln file. Later, reading the feature list of Mono and Monodevelop, I saw that Monodevelop fully supports MSBuild and even defaults to it. Nice! Embrace, extend, and... er... isn't that was Microsoft is supposed to do? Interesting to see somebody else doing it!

I replaced the old SpecUnit reference with the new one, compiled... And BAM! Another error! This time, the compiler was not too happy with my use of the extension method ShouldEqual, which was otherwise recognized correctly by the editor. However, the compiler kept complaining. After double-checking that all my projects were compiling for the .Net 3.5 framework, I googled the problem and found out that you have to include System.core in your references, which is not automatically done by Monodevelop. And that did the trick, the solution compiled!

I just had commited a heresy. I had just put a DLL on my pure, pristine Linux system. A DLL! No, even worse: two DLLs! I grapsed for something to hold on to. When the oily feeling that tainted my soul finally subsided, I ran the test:

It passed!

Now, this is really cool! I look forward to doing more with Mono. Hopefully, more and more developers will start using it. So far, the experience has been good, but I miss ReSharper and the ability to run tests from within the IDE. Monodevelop allows us to write plugins, so anybody up for a a R# clone on Monodevelop?

 

Thanks for reading.

Kick It on DotNetKicks.com
Posted May 25 2009, 09:01 PM by Louis Salin

Comments

Michael Koby wrote re: Learning how to walk with Mono
on 05-25-2009 11:15 PM

Mono has certainly come a long way since it first started.  There is an excellent team behind the project and it's compatibility can be astounding at times.  I'm running mono and monodevelop on Ubuntu 9.04 and the only downside is it isn't 2.4 by default.

To run tests inside the IDE, you'll need to install the monodevelop-nunit plugins (that's what they're called in Ubuntu not sure about OpenSuSE).  

Louis Salin wrote re: Learning how to walk with Mono
on 05-25-2009 11:42 PM

Thanks for the tip about the monodevelop-nunit plugin!

Benoit LeBlanc wrote re: Learning how to walk with Mono
on 05-26-2009 9:09 AM

Isn't MonoDevelop also available on Mac OS ?

I'm tempted to check that out... I just don't do enough .NET at my job and I'm starting to crave doing some again.

Hey buddy, need any help on your project? :-)

AllanN wrote re: Learning how to walk with Mono
on 05-26-2009 9:55 AM

Mono is great.  The one big show-stopper that I've run into is lack of IDE debugging support for ASP.Net.  Back to stack trace and debug.print debugging, not fun.  Now, I understand that they're working on remote debugging support - debug your mono/linux asp.net app from your windows PC.  That would be good for debugging configuration issues on your production machine (linux), but i doesn't make it easier to use linux as your dev platform.  I also understand that IDE debugging support for asp.net is in the works as well, but last time I checked (a couple months ago?) I couldn't tell when that might come about...

DotNetShoutout wrote Learning how to walk with Mono - Louis Salin's Blog - Los Techies
on 05-26-2009 10:30 AM

Thank you for submitting this cool story - Trackback from DotNetShoutout

Louis Salin wrote re: Learning how to walk with Mono
on 05-26-2009 10:47 AM

Allan, I can feel your pain. I haven't gotten to debugging yet and I hope I can mitigate those problems by doing good BDD. However, I don't think I'll ever do Asp.Net on Linux and I think there is a debugger for regular desktop apps.

Michael Hutchinson wrote re: Learning how to walk with Mono
on 05-26-2009 2:15 PM

Louis, I'm glad you like it! Lots of people seem to be interested in writing refactoring addins, and I have heard a few of them cite ReSharper as an inspiration :)

AllanN, MonoDevelop trunk supports ASP.NET debugging already. Debugging as[pc]x files still has a few issues, but as long as you use CodeBehind that shouldn't be a problem.

Casper wrote re: Learning how to walk with Mono
on 05-26-2009 2:28 PM

Very nice intro, I hope you will continue.

Marco wrote re: Learning how to walk with Mono
on 05-27-2009 2:48 AM

Mono is bad because .NET is bad. Why .NET is bad ? It's simple, because it comes from M$.

FULL STOP

samnang wrote re: Learning how to walk with Mono
on 05-29-2009 5:24 AM

Nice post, could you guide me how to get that on ubuntu 9.04?

Louis Salin wrote re: Learning how to walk with Mono
on 05-30-2009 11:27 AM

samnang, the reason I went wit OpenSuse as my distribution is that I had previously tried to install monodevelop on Ubuntu and it just wouldn't work. When searching the Internet for an answer, it seemed all I could find was that you couldn't install MonoDevelop 2.0 on Ubuntu yet.

gOODiDEA.NET wrote Interesting Finds: 2009 05.24 ~ 05.31
on 06-07-2009 8:25 PM

.NET Generic Method for Loading Interfaces in C# (For a Plugin System) Which came first, the View or

Louis Salin's Blog wrote Gtk# for .Net developers
on 09-29-2009 9:54 PM

This is going to be my first post in a series about my experience using Gtk#, a choice I made a few months

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems