Currently Browsing Posts Tagged C#

Structuring Unit Tests

One of the most troubling parts of unit testing, to me, is keeping things organized. For the past year, I’ve been managing my tests like this:

  • Every class has a tester class; for example, MyClass would have MyClassTests
  • In MyClassTests, multiple test methods would be setup to test each of the methods in MyClass
  • Test names would be in this format: MethodName_Scenario_ReturnValue

Tests for large classes would get out of control! There could be several test methods for a single method on the original class. Imagine a tester class if the original class had 20 methods.

However, thanks to this excellent tip from Phil Haack, this situation can be cleaned up and put into an easier to manage form.

By structuring your unit test classes to contain nested classes, each designated to test a single function, you can have a set of tests look like this:

For a further explanation, I would highly recommend reading Phil’s post: Structuring Unit Tests. I’m going to be trying this out with my testing environment (I use nunit) over the next few weeks.

Sanitizing Phone Numbers With C#

An extension method to remove all but digits from a phone number. This should be useful for sanitizing input to put into your database.

using System.Text.RegularExpressions;

namespace CustomStringMethods
{
	public static class PhoneNumber
	{
		#region Class Methods

		public static string StripAllButDigits(this string s)
		{
			return (s == null) ? string.Empty : Regex.Replace(s, @"\D", string.Empty);
		}

		#endregion
	}
}

Obviously this can be used for more than just phone numbers.

XMLUrlResolver: Using Embedded XSLT Resources in C#

Over the last week or so, I have been searching for a method to properly include XSL files via xsl:include within a .NET embedded resource. Apparently, using GetManifestResourceStream() (via an Assembly) wasn’t good enough (it wouldn’t follow the xsl:includes, simply ignoring them). Luckily, I came across a (semi-)working solution over at Signs on the Sand. Now, the concepts behind this were right, it just wasn’t working for my particular situation. Here’s the scoop:

Continue reading…

What I’ve Learned So Far

So far, my internship has been going great! I’ve had many opportunities to work in a new environment, and I’ve been able to tap into one of my more underused skills: programming. Since I’ve been working for Independent, I’ve been able to learn a lot of new concepts; not only in programming, but in some of the operations of a business. Here is a brief overview of some of the things I have learned, or am learning.

Continue reading…