Day 2 – Dot NET on Linux – Unit testing with NUnit in MonoDevelop

Intro

Unit testing should play a major part in any software development and we are not going to cut corners here.

The Source Code

This post is part of my Dot Net On Linux posts. All the source the source code from the series of posts is available on GitHub here

Why Unit Test

Unit testing is an important part of any source branch. I personally prefer TDD; it makes you think about  the code you are writing. Understanding how to test the code you are writing allows creation of better structured, better working and provides less chance of introducing bugs during the codes lifetime. Fitting unit tests into code post development often requires refactoring to allow testing of expensive entities such as the database. Refactoring without unit tests requires a complete understanding of the code being edited; which is not always possible. In conjunction with mocking frameworks we can unit test our code without a database or other expensive resources.

Why NUnit

MonoDevelop comes with a NUnit add in module which allows integration of NUnit and MonoDevelop; we can run and validate unit tests in situ.

Installation

To install the NUnit plugin run the following in a terminal or install via your package manager such as Synaptic.

sudo apt-get install monodevelop-nunit monodoc-nunit-manual

 

Setting Up

Create a new solution and call it MathsLibrary.

Create a new test project by right clicking on the MathsLibrary solution and selecting Add –> Add New Project.

If the option is not available make sure you are on the solution and not the project or the Work Space item. One of the annoying things about MonoDevelop is that the many menu items are context driven.

From the new project dialog select NUnit Library Project.

Image

Add in a reference to the NUnit framework;  highlight the unit test project, right click and select Edit Reference. In the edit references dialog add in nunit.framework assembly from the packages tab. If you have multiple versions add the highest version.

Image

The Code

Test classes requires the TextFixture attribute, test functions require the the Test attributes.

using System;
using System.Collections.Generic;
using MathsLibrary.Average;
using NUnit.Framework;

namespace MathsLibrary.Tests
{
	[TestFixture()]
	public class MeanTests
	{
		[Test()]
		public void CanCalculateMean()
		{

			var data = new List{ 1.1m, 2.2m, 3.3m, 4.4m, 5.5m };

			var modeValue = Mean.Calculate(data);

			Assert.AreEqual(modeValue, 3.3);
		}

		[Test()]
		public void WillThrowArgumentNullExceptionWhenNull()
		{
			Assert.Throws(() => Mean.Calculate(null));
		}

		[Test()]
		public void WillThrowArgumentExceptionWhenEmptyCollection()
		{
			Assert.Throws(() => Mean.Calculate(new List()));
		}
	}
}

You would need to write the code stubs on the other side and stub them by throwing a NotImplementedException.

Image

Running The Tests

We are ready to run our tests. View Menu –> Pads –> Unit Tests

To run all tests –> Hit the up arrow

To run all tests contained by the selected parent tree element –> Hit the cog wheel or right click and –> Run Test.

To run the test in debug right click a test in the  tree view and select –> Run Tests With –> Mono Soft Debugger. If you have created any break points they will be hit.

To add a break point you need to click the line in the far left of the margin.

Two other handy elements are Right Click –>Go To Failure / Show Test Source Code.

Image

You then need to write the code you are testing and then run the tests again.

Image

Note: To use Linq you need to add a reference to System.Core.

The only other worth note is the results pad which provides some basic analysis of the test run.

Image

Day 1 – Dot NET on Linux

Intro

Before starting to code in ASP.NET MVC V3 I wanted to test the waters with MVC V2.

The Source Code

This post is part of my Dot Net On Linux posts. All the source the source code is available on GitHub here

The MVC Quick Hack

I won’t go into much detail. I wanted to initially have a quick play around with MVC 2 to have a feel for what was working.

MonoDevelop had a few issues; models were not displayed in the add view dialogue box and it was incorrectly reporting views were already existing upon creation. A few missing HTML binders appear to be the only incomplete API I could find; over all pretty impressive.

Day 0 – Dot NET on Linux – Installing Mono and MonoDevelop

The Source Code

This post is part of my Dot Net On Linux posts. All the source the source code is available on GitHub here

Latest Mono & MonoDevelop Repository

Information on a repository with the latest Mono and MonoDevelop, for all Linux flavors, can be found here.

Intro

Before we start coding we need a development environment.

I am using LMDE which has more upto date environments than LM or Ubuntu. It even supports the Entity framework

Installing

Open up Synaptic and install the following packages.

  • monodevelop (3.0.3.2)
  • mono-xsp2
  • mono-xsp4
  • mono-gmcs
  • monodoc-browser,
  • asp.net2-examples

Alternative you can copy and paste the following command into a terminal

sudo apt-get install mono-mcs mono-xsp2 mono-xsp4 monodevelop monodoc-browser asp.net2-examples

What have I installed?

  • MonoDevelop is an IDE (Integrated Development Environment) for a number of languages and technologies.
  • Mono-xsp2 is an ASP.NET 3.0/4.0 Web Server.
  • Mono-gmcs is a C# 2.0 and C# 3.0 compiler. The default MVC setup which comes with Mono wanted to use .NET 3.5 while the .Net 4 compiler came as standard.
  • Monodoc-browser allows us to see the compiled Mono development documentation; on-line version is available.
  • ASP.net2-examples is a web site coded in ASP.NET which is used to show some examples of using the ASP.NET API.

This will allow us to code, compile and deploy in C# .NET 2/3/4 as well as create ASP.NET, ASP.NET MVC website and run them locally.

Running

Just to prove that we have installed everything I am going to run the XSP2 web server pointing at the ASP.NET 2 example projects which we have previously downloaded.

cd /usr/share/asp.net-demos/
xsp2

To view the example web sites copy this into your browser.

http://localhost:8080

And we have ASP.NET 2 Web site running on Linux

Developing C# ASP.NET MVC & WCF Web Services in Linux

I am not sure what I am trying to prove in these articles. Personal ‘learning development’ is always high on the agenda being a software developer, however making the programming skills I learn in my spare time applicable to my job as a .NET Developer is sometimes a hard choice being such a Linux enthusiast.

On one side I would like to see if it is possible to write and deploy an ASP.NET MVC website on Linux, while on the other hand I would like to develop the skills which are applicable to my job.

Hopefully once I have tested the water with these skills in Linux, I will be able to think of a worth wile personal development project which can provide me a bases to improve to the skills needed in my job while satisfying my hobby of coding under Linux.

Below is a list of skills, technologies and frameworks which I use daily in my job. I would like to improve these skills in some way; even if it is just to see if I can code these in Linux.

  •     C#
  •     ASP.NET MVC
  •     ASP.NET
  •     ADO.NET
  •     Unit Testing
  •     Mock Testing
  •     Source Control
  •     Database Access
  •     WCF Web Services
  •     SQL
  •     Generics
  •     Linq
  •     Lambda Expressions
  •     Object Relational Mapping (NHibernate/Entity)
  •     JavaScript/JQuery
  •     CSS

The mission plan

  1. Provide a series of How-To articles providing insight into my first exploit of developing .NET technologies under Linux.
  2. Provide the source code on line.
  3. Hopefully to provide help to someone else by writing about my experience.