Monthly Archives: March 2011

There are certain situations where you may want to deploy files permanently to a computer so that they persist through uninstall/re-installs or upgrades. A typical situation in my business is we’ll deploy a config file with default settings. If the settings change, we don’t want them to be lost or overwritten during an upgrade.

By making a very simple modification to your Wix installer, we can get this behavior.

Share this:

Like this:

I have been a Windows Phone user since WP7 was released at the end of last year. From the very beginning, there have been rumors of updates coming in Janurary, February, March… but now, on the brink of April, I’m still yet to get any updates.

I’m not upset about the delay because I really don’t have any major gripes and am generally happy with my phone. At the same time, I want them updates!

Last week, I was excited to see Microsoft introduce a “Where’s my phone update?” site where you can check on the status of the phone updates by region and phone model. This is great because I can check the website periodically to see when it moves out of Testing, to Scheduling, and finally to Delivering Update.

Share this:

Like this:

I was out at a customer site a few weeks ago and needed to whip up a quick program to extract a large number of values from an XML document. To me, the obvious way to accomplish this task was to write a quick application to read the XML, parse the values, and write them to a separate document. Linq to XML made this cumbersome task very easy, and I was reminded of just how amazing Linq to XML really is.

Linq to XML is one of the best things to happen to XML in recent history, but I think there are still a lot of developers out there who are either not familiar with Linq or are just comfortable working with XML using the same techniques that they always have. I haven’t done an extensive amount of XML processing in my career, but I haven’t exactly been a stranger to it, either. In my opinion Linq to XML makes working with XML downright trivial. If you haven’t looked into it, I definitely recommend that you do!

For the purposes of demonstration, consider an XML file “data.xml” with the following contents:

Share this:

Like this:

I was speaking with some co-workers earlier this week, and someone mentioned you own all variations of your Gmail email address that contain different period placements. This means that if you register joe.blow@gmail.com, you also own joeblow@gmail.com, j.o.e.blow@gmail.com, and joe.bl.ow@gmail.com. You can verify this two ways. First, you can log in to Gmail using any of these variations; it will still take you to the same inbox you’re used to seeing. Second, you can send an email to any of these variations, and you’ll receive it just like you’d expect.

The usefulness of this is questionable, but I still thought it was fun and interesting to know. And maybe you wished you could make your gmail more specific (for example, nycanal@gmail.com could use ny.canal@gmail just so there’s no confusion).

This reminded me of another interesting trick that you can do with your Gmail account. Google let’s you append “+” and text to your email address. I love using this when registering for different things. When the email comes in, the tag will be included. So, if you starting getting spam in your inbox sent to joe.blow+bestbuy@gmail.com, you’ll know where they got your email address. More importantly, you can easily set up an email filter for messages sent to that address so you don’t need to worry about them anymore. The same is true for the period-placement variations, too; you’ll receive the emails with the To line containing the address they were sent to, so you can filter or what-not as you please.

The Add method is so simple that there isn’t much more you can do with it, so let’s take a look a the Divide method. Similarly to the Add method, we can write a simple unit test to verify our core behavior.

Now that we have that out of the way, let’s deal with an exception case: division by zero. Before we can test or code, we must decide what we want our application to do in this scenario. I have decided that application should simply throw the default divide-by-zero exception, so let’s write a test for it to help ensure that this functionality isn’t lost with future changes.

By decorating the test method with the ExpectedException attribute, the test will fail if the exception is not thrown.

You can see that writing unit tests for these something-in-something-out functions can be very easy. Try to test as many different scenarios as you can come up with. Some common situations to consider are minimums, maximums, default values, and nulls. Write skeleton tests up-front to help you remember relevant scenarios to be handled by your code.

Share this:

Like this:

There always seems to be a lot of discussion regarding the whens and hows of unit testing. These can be challening topics when you’re getting started with unit testing, and I wanted to document some of the rules, guidelines, and lessons-learned that I apply when writing my own tests.

Keep it simple.

If it seems like testing a piece of functionality is really hard, there’s probably an easier way. One of the keys to writing good unit tests is having testable code. (Duh, right?) Well, one of the best skills that you can have is the ability to take a block of untestable code and transform it into a well-tested masterpiece. Doing this isn’t as hard as it may seem. The secret is identifying the natural seams in the code and extracting them into testable and/or mockable objects and functions. Divide and conquer!

It’s not all or nothing.

Sometimes when you’re working with legacy code that doesn’t have unit tests, it’s easy to follow suit and not write tests yourself. Just because tests don’t exist for existing code doesn’t mean that you can’t write tests for your changes, though. If you write a test to confirm a change that you make, you’re future-proofing your change. If somebody comes along later and messes up your change, your test will fail even if it’s the only test in the whole world.

It’s been said before, but it’s true: one test is better than no tests. It’s also unlikely that you’ll ever go back and unit test everything in a project hat wasn’t tested previously, so just start already!

Test what makes sense.

Code coverage is great, and 100% is a great goal to strive for, but I don’t think it’s realistic in most situations. If you’re writing a test where you’re replicating the entire contents of the function you’re testing to create expectations and asserts of the test, it may be a bad test for two reasons. First, you don’t feel good about the test you’ve written. Second, you’ve made your code more rigid because changing the actual function will require you to make the same change again in the unit test. Blech!

Instead, strive to write tests that you believe are meaningful and useful. A good example of this would be translating a person object. It may not be useful to test that ObjectA.LastName equals ObjectB.LastName and ObjectA.FirstName equals ObjectB.FirstName, but it may be worthwhile to write a test that verifies that ObjectA.SsnAsInteger populates correctly into ObjectB.SsnFormattedString. It’s hard to come up with a good, brief example of this, but what I’m trying to get at is that you shouldn’t spend hours and hours working on tests that you yourself see no value in. Grab a peer and look at it together. Maybe they can help you see the value or come up with a better way to test. Or maybe you’ll both agree that the code in question simply doesn’t need to be tested.

Don’t write random value generators and object populators.

I’ve gone through this phase, and I’ve seen others go through it. I want to save you, though. Don’t do this. I know that it seems like a good idea. This should work for any string of any length, so I’ll just write a function to populate my object with a random string of random length! The problem is that when this test fails randomly 1 out of 50 test runs, you may not be able to re-create the situation. I believe it’s better to write a specific test for your specific situation. If you need to test multiple scenarios, write multiples tests.

One of my least favorite unit testing experiences comes from working with a project whose unit tests were written by a code generator that randomly populated complex source business objects, manipulated them, and then used comparison-via-reflection to verify correctness. The problem is that these tests were nearly impossible to maintain and troubleshoot when they failed.

Share this:

Like this:

Hi, I’m Adam. This is my blog.

I started this blog as a personal searchable repository of things I've learned and figured out. It evolved into a mechanism to facilitate personal growth, and now it's turned into a bit of a hobby. I enjoy writing about software development, my professional life, and related topics.

I love to hear from readers, so please feel encouraged to leave comments on anything you read here.

Follow Blog

Enter your email address to follow this blog and receive notifications of new posts by email.