Wednesday, February 25, 2015

Daily Lesson 1 of 2 : Test Project in .NET

Make sure that your test project has the same .NET framework setting as the code it's testing.

I was working through an issue with emails not being sent via a windows service running on a loop with a heartbeat to process emails from a DB table queue. Design issues aside, there was an error being logged about the set operation of the .NET MailMessage Subject property.

In order to debug the issue, I whipped up a quick test project in Visual Studio with a single test class with a single method. The method set the Subject property in a for each loop with a try-catch-throw wrapped around the set call. The throw generated a new exception with the current subject from the loop so I know which one was causing the issue.

Next, I grabbed all the queued subjects with a SQL statement which appended " to the front and ", to the back of all subjects in the queue table. I ran the query, copied the results, pasted into the test inside the array initializer. Ran the test and milliseconds later it passed. WTF!?

I examined the MailMessage class from the original and noticed it was from .NET 2 and the one in my test proj was from .net 4. Duh, use the same version of .NET in the test proj as the source code version. So I changed the version in the test project to 3.5 and now it was in synch.

Ran the test again and BAM passed again...seriously!!? Lesson Learned 2 of 2 tells why.