Geoffroy’s Weblog

November 10, 2010

Unit Testing with Visual Studio 2010

Filed under: Unit Testing, Visual Studio 2010 — gseive @ 11:56 pm

Things have improved quite a bit since VSTS 2008 beta when I wrote a post on unit testing private methods.

It appears that both public and internal classes, and methods from public to private can be unit tested in a fairly straightforward manner.

To illustrate that, I coded a simple application and highlighted the unit testing of a private method.

Let’s say I want to create a simulation of the Monopoly game.

I create a new solution with two projects to begin with.

In a console application project the startup code could look like this:

const int numberOfPlayers = 3;
var game = new MonopolyGame(numberOfPlayers);
game.Play();

In a library project the code for the MonopolyGame class could look like this:

    internal class MonopolyGame
    {
        private readonly int numberOfPlayers;
        private const int MinimumNumberOfPlayers = 2;
        private const int MaximumNumberOfPlayers = 8;
        private List<Player> players;

        public MonopolyGame(int numberOfPlayers)
        {
            this.numberOfPlayers = numberOfPlayers;
        }

        public void Play()
        {
            if (this.HasRequiredNumberOfPlayers())
            {
                this.players.ForEach(player => player.TakeTurn());
            }
        }

        protected bool HasRequiredNumberOfPlayers()
        {
            return numberOfPlayers >= MinimumNumberOfPlayers && numberOfPlayers <= MaximumNumberOfPlayers;
        }
    }

Then we add a Test project to the solution.

And add a unit test with the Add / Unit Tests… menu option. Not Add / New Test… which would not generate everything we need for us. (This is something to explore: how to do it all manually?)

This  adds all the references and the test reference needed. It adds the unit test class and auto-generates some code for us.

This is what the code looks like for the HasRequiredNumberOfPlayers() test method:

/// <summary>
///A test for HasRequiredNumberOfPlayers
///</summary>

[TestMethod()]

[DeploymentItem("MonopolyGameBL.dll")]

public void HasRequiredNumberOfPlayersTest()

{

PrivateObject param0 = null; // TODO: Initialize to an appropriate value
MonopolyGame_Accessor target = new MonopolyGame_Accessor(param0); // TODO: Initialize to an appropriate value
bool expected = false; // TODO: Initialize to an appropriate value
bool actual;

actual = target.HasRequiredNumberOfPlayers();
Assert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");

}

Initializing the way we need to, we get the following code:

        /// <summary>
        ///A test for HasRequiredNumberOfPlayers
        ///</summary>
        [TestMethod()]
        [DeploymentItem("MonopolyGameBL.dll")]
        public void HasRequiredNumberOfPlayersTest()
        {
            int numberOfPlayers = 3; 
            MonopolyGame_Accessor game = new MonopolyGame_Accessor(numberOfPlayers); 
            bool expected = true; 
            bool actual;
            actual = game.HasRequiredNumberOfPlayers();
            Assert.AreEqual(expected, actual);
        }

This can be simplified a little bit:

        /// <summary>
        ///A test for HasRequiredNumberOfPlayers
        ///</summary>
        [TestMethod()]
        [DeploymentItem("MonopolyGameBL.dll")]
        public void HasRequiredNumberOfPlayersTest()
        {
            int numberOfPlayers = 3; 
            var game = new MonopolyGame_Accessor(numberOfPlayers); 
            Assert.IsTrue(game.HasRequiredNumberOfPlayers());
        }

Now, what do I do if I want to test another valid value and let’s say a few that are outside the valid range (at least one under and one above)?

I see three options.

Notes:

You can enable specific other assemblies to access your internal types by using the InternalsVisibleToAttribute. For more information, see Friend Assemblies (C# and Visual Basic).

November 9, 2010

Debugging Tools for Windows

Filed under: Process Explorer, Windows 7 — gseive @ 10:51 pm

Debugging Tools for Windows; link for reference:

http://www.microsoft.com/whdc/devtools/debugging/default.mspx

I installed it both on a 32-bit Windows 7 machine and a 64-bit Windows 2008 server. Apparently, the Debugging Tools option under the Common Utilities is the one that allowed Process Explorer to show me the threads of a process. This is really the ultimate icing on top of the Process Explorer cake!

November 2, 2010

Static Methods are a Code Smell

Filed under: Coding Best Practices — gseive @ 8:31 pm

Resharper signals those methods in a class that can be made static. It’s rarely a good idea to follow blindly its advice. In an object oriented system work gets done by objects collaborating with one another. Behavior happens at the instance level. What happens at the class level should be the exception rather the norm.

http://searchwindevelopment.techtarget.com/tip/0,289483,sid8_gci1395284,00.html?track=NL-150&ad=752477&asrc=EM_USC_10977590&uid=6036330

It’s time!

Filed under: Ideas, Metric System — gseive @ 8:18 pm

In January of 2069, Anna and Mike have their grandchildren over. Once again, they just can’t help sharing with them one of the biggest events of their lives.

“In 2010, we were just kids, but we still remember very vividly the event of the century. Our parents elected our first black President. His plate was more than full. He did many great things for his country. Maybe the one thing that has had the most long-lasting effect is not the one that received the most press coverage at the time. Back then, it didn’t even sound as if it were the most important. His announcement did bring some controversy, for sure, but it was presented with great mastery.”

“After two centuries of talking about it, we are finally going to fully embrace the metric system.

Industry needs simplification.

Youth needs excitement.

Let’s break the isolation, join the rest of the world, and leapfrog into the future.

We will all benefit from it in ways that we just don’t realize today.

How many industries won’t need to manage double lines of products anymore? Did you know it is going to make your doctor happy?

Because of the profound impact it can have on our lives, and the stimulus it can bring to our economy, this event is as big as putting a man on the Moon!

Wall Street moved to the decimal system.

Sweden moved from the left side of the road to the right side.

Europe moved to the Euro.

India moved to the metric system.

We have a roadmap, and we will execute it rapidly.

It’s our turn now to show the rest of the world that we can do it!”

Create a free website or blog at WordPress.com.