Rhino.Mocks, TDD

The main reason to TDD; it’s not unit tests.

I see that a lot of people using unit tests as a main selling point for TDD.  I disagree with that.  I believe that the main strength of TDD practice is that developers are forced to write loosely coupled code.

I don’t want to minimize the importance of unit tests. They’re the safety net and also a great reason to use TDD but you don’t need to follow TDD practice to create unit tests. 

I got inspired to write this after reading ‘Good practices are beyond testing’ article.  The author of this post makes a point that if we have mocking frameworks that let us to take shortcuts than what is the reason for TDD. Here’s a quote from the article.

If we don’t need DI or interfaces for testing, then why do we need them? If testing can happen without it, then that behavior isn’t driven, right? Why can’t I make a big class if its testable?

I personally switched from TypeMock to Rhino Mocks because TypeMock didn’t force me to write loosely coupled code.  I talked about this in one of my previous posts.

Unit tests are must have but it’s not the reason we TDD.

.Net, C#, Coding, MbUnit, TDD

Explaining MbUnit GUI tree.

When the first time I looked at MbUnit GUI tool, I was overwhelmed with complex tree structure. The root has five branches: Authors, MbUnitGuiColapsedTreeCategories, Importances, Namespaces, and TestsOns. I was able relate to Namespaces and Categories; I’ve seen something similar in NUnit. But what for are other three branches. Well, MbUnit GUI shows the branches in alphabetical order. There for I’ll try explain the branches in the same order. I’ll start with Authors and end with TestsOns.


It’s pretty clear that if you want to claim ownership of the test fixture you need to put it inside the Authors. It’s not hard to do. What you need to do is to use Author attribute on your fixture. Author attribute has three overloads:

   1: [TestFixture]

   2: [Author("Vadim")]
   3: public class MyTest {}


   1: [Author(“Vadim”, “vadim@domain.com”)]

   2: [Author("Vadim", "vadim@domain.com", https://vkreynin.wordpress.com/)]

MbUnitGuiAuthors Before we added Author(“Vadim”), we had “Anonymous” leaf inside the Authors branch. “Anonymous” always will have test fixtures that don’t have an Author attribute. I believe it’s a good practice to use this attribute on big projects. However, instead of a person name I would use a team name. The reason I think it should be a team name because a developer can switch a team, find a better job or win a lottery and leave the team. A team is responsible for the part of the project they are working on and there for they also should be responsible for the tests. Also a team probably would like to run all the tests related to the team before checking in the code.


Categories branch is obviously for categorized test fixtures. You can assign multiple categories to a test fixture. Here how you do it using FixtureCategory attribute.

   1: [TestFixture]
   2: [FixtureCategory("Demo")]
   3: [FixtureCategory("WebPayroll")]


All the test fixtures that don’t have a category assigned can be found in Categories/Misc branch.


This branch specifies the importance of the test fixture. MbUnit Framework defines five different types of test fixture importance.

  • Critical
  • Default
  • NoOneReallyCaresAbout
  • Serious
  • Severe

Importance can be defined by Importance attribute.

   1: [TestFixture]
   2: [Importance(TestImportance.Severe)]

The test fixtures that don’t have importance can be found in Default importance. To be honest I didn’t find it very useful yet. Let me know how you use this attribute.


I don’t think that I need to explain this. Anybody who develops using .NET know what Namespace is.


For some reason this one took me the longest to figure out. Using TestsOn attribute you can specify what class the test fixture is testing.

   1: [TestFixture]
   2: [TestsOn(typeof(Employee))]


In the example above we are saying that the test fixture is testing Employee class. All the test fixtures that don’t use TestsOn attribute can be found in Unknown branch.

kick it on DotNetKicks.com


Watch out for escape characters.

You have to be aware of the escape characters when you parse a file  character by character.  For instance if you try to read a file that contains “C:\bigFile.txt” you’ll get back “C:igFile.txt” or something like this.

If you don’t believe me, try it yourselves.

   1: public void ParseString()
   2: {
   3:     string text = "C:\bigFile.txt";
   4:     string newText = string.Empty;
   5:     foreach (char c in text)
   6:     {
   7:         newText += c;
   8:     }
   9:     Console.WriteLine(newText);
  10: }

Below I provided a table of escape character for you and myself to reference later.

Char ASCII Dec# ASCII Hex# ASCII Name Ctrl Char Description
00 00 NUL ^@ null
\a 07 07 BEL ^G Alert
\b 08 08 BS ^H Backspace
\f 12 0C FF ^L Form feed
\n 10 0A LF ^J New line
\r 13 0D CR ^M Carriage return
\t 09 09 HT ^I Horizontal tab
\v 11 0B VT ^K Vertical tab
\” 34 22 N/A N/A Double quote
\’ 39 27 N/A N/A Single quote
\\ 92 5C NA N/A Backslash
\u#### N/A N/A N/A N/A Unicode escape
\x## N/A N/A N/A N/A Unicode escape
\Uxxxxxx N/A N/A N/A N/A Unicode escape

Let me know if I miss anything.

iPod, iTunes

Podcasts in iTunes don’t sync to iPod video.

I just spend about 20 minutes to figure out why podcasts in iTunes are not transferred to my iPod.  To fix the problem I had to do following steps.PodcastsSync

1. Connect the iPod.

2. Click on Podcasts tab.

3. Make sure that Sync checkbox is checked.


I spent about 20 minutes to figure out the problem and about 10 minutes typing this post.  I hope that it saves time for you.

.Net, C#, Coding, MbUnit, TDD

MbUnit: Testing Internal classes

Jonathan ‘Peli’ de Halleux wrote an article ‘Pex It: Testing Internal classes‘.   I just want to let the world know that you also can test non-public classes with MbUnit.  You need  version or higher to use this feature.

In example bellow we test IsWhite(Char) method of System.Number class inside mscorlib assembly.  Using Reflector you can see that Number is an internal class and IsWhite is a private method of this class.  We can test this method event if we don’t have the source code.

private static readonly string MSCorLibAssembly = 
    + @"\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll";
public void InternalClassTest()
    string className = "System.Number";
    object obj = 
        Reflector.CreateInstance(MSCorLibAssembly, className);
            AccessModifier.Default, obj, "IsWhite", ' '));
            AccessModifier.Default, obj, "IsWhite", 'V'));

The Number class has only default constructor.  It means we don’t need to send parameters when creating an instance.

Let’s look at another example.  This time we’re going to write test for the Key and Value properties of System.Collections.KeyValuePairs class.  This class is also internal.  If you don’t believe me, use Reflector.  KeyValuePairs class has only one constructor that takes two Object parameters.

public void InternalClassNonDefaultConstructor()
    string className = "System.Collections.KeyValuePairs";
    object obj = 
        Reflector.CreateInstance(MSCorLibAssembly, className, 1, 'A');
    Assert.AreEqual(1, Reflector.GetProperty(obj, "Key"));
    Assert.AreEqual('A', Reflector.GetProperty(obj, "Value"));

 Happy Testing :)

.Net, C#, Coding, Visual Studio

Get Array type.

Let assume that you have an int array.

int[] intArr = new int[] { 1, 2 };

If you try to get the type of the array, you’ll get int[] type.

Assert.AreEqual(typeof(int[]), intArr.GetType());

So far so good.  But what if code needs to perform different logic for array of ValueType and Reference Type elements.  We all know that int is a ValueType. However, array of int is not. So the if statement below will output ‘Reference Type’.

if (intArr.GetType().IsValueType)
    Console.Write("Reference Type");

Microsoft provided us with GetElementType method that returns the Type of the object encompassed by the array.

if (intArr.GetType().GetElementType().IsValueType)
    Console.Write("Reference Type");

The code above outputs ‘ValueType’ as expected.

.Net, C#, Coding, Visual Studio

Using Predicate delegate

Have you ever try to find an element in the List or Array.   I’ve done it many times.  However, every time I have to look for an element inside a List I don’t remember how to do it and I have to either Google or search my own code.

This time I have to FindLast element in my List and I don’t remember how to do that.  So I decided to put it here so next time I know where to find an example.

public void PredicateExample()
    List<string> myLst = new List<string>(
        new string[] { "elem1", "elem2", "elem3" });
    string elem = myLst.Find(delegate(string elemInSearch) 
        { return elemInSearch == "elem2"; });
    Assert.AreEqual("elem2", elem);

You also can create predicate delegate as separate method:

public void AnotherPredicateExample()
    List<string> myLst = new List<string>(
        new string[] { "elem1", "elem2", "elem3" });
    string elem = myLst.Find(FindElement("elem2"));
    Assert.AreEqual("elem2", elem);
private Predicate<string> FindElement(string elem)
    return delegate(string listElem)
           return listElem.Equals(elem);

I hope it helps you.  It definitely is going to help me.