testing comments edit

I follow the whole “design-for-testability vs. design-for-usability” debate and, in the interest of full disclosure, I’m a fan of not designing for testability. Part of what I design has to be a usable API and I can’t have a lot of the cruft required in testable design when I get to my finished product.

I mean, think about it - if the .NET Framework was built using design-for-testability, there’s no way like 80% of the people using it today would have been able to pick it up. Considering the inability to solve a FizzBuzz problem, somehow I don’t think having everything infinitely pluggable and flexible solely to support the testing of the framework would have made it any easier to use.

Now think about the notion of the static utility class. Everyone has one (or more) of those classes that are sealed and only provide a few static helper methods. I mean, for .NET 1.1 folks, who didn’t have their own static String.IsNullOrEmpty implementation?

On my project, we have a lot of static helpers that read, process, and cache configuration data. The basic algorithm is something like this:

  1. Check the cache for the data. If it’s there, return it.
  2. Data wasn’t in cache, so open the config file.
  3. XML deserialize the file or do whatever other processing needs to happen.
  4. Cache and return the result.

Here’s some basic sample code: public sealed class ConfigService { private const string CacheKey = “CacheKeyForConfig”; private const string ConfigPath = “/Config/myfile.config”; public static System.Collections.ArrayList GetConfiguration() { System.Collections.ArrayList retVal = null; string configPath = ConfigPath;

    if(System.Web.HttpContext.Current != null)
      retVal = System.Web.HttpContext.Current.Cache[CacheKey]
        as System.Collections.ArrayList;
      configPath = System.Web.HttpContext.Current.Server.MapPath(configPath);
    if(retVal == null)
        throw new FileNotFoundException(
          "Unable to read default configuration file.",

      //... read/process the file... set the return value...

      if(System.Web.HttpContext.Current != null)
          new System.Web.Caching.CacheDependency(configPath));

    return retVal;

From an API standpoint, it’s a one-liner: ConfigService.GetConfiguration()

But how do you test that? If you’re running FxCop, your static utility class “ConfigService” needs to be sealed. That sort of limits your abilities to mock with several frameworks out there. The simple fact this is a static method is limiting to some frameworks.

Now, granted, you could write a class that provides cache retrieval services specific to this helper and go to the trouble of instantiating that class and… you know what, I’m already tired of typing that out. I don’t need all that. I’ll never sub in a different cache provider for anything other than testing. I don’t want the consumer of the method to even have to know about any of that (so they shouldn’t have to pass the cache instance in as a parameter, for example).

But I do want to have this thing tested. If the object is in cache, does it just return the object without further processing? If it’s not, does it read the file and does it then cache the results? I want the design simple, I want it usable, and I don’t want a lot of moving pieces. In fact, ideally, the code would be about as simple as the sample I posted.

So you need to mock a few things, specifically around HttpContext. (Possibly other things based on the implementation, but we’re going for simple here.)

You can’t really readily do that. Or can you? What if your test looked like this:

public class ConfigServiceTest
  public void GetConfiguration_NoCache_FileExists()
    //...place a known good configuration in a temporary location...
    string pathToTemporaryConfig = ExtractConfigToTempFileAndGetPath();

    // Set up calls for the cache
    MockObject mockCache = MockManager.MockObject(typeof(System.Web.Caching.Cache), Constructor.Mocked);

    // Set up calls for the server utility
    MockObject mockServer = MockManager.MockObject(typeof(System.Web.HttpServerUtility), Constructor.Mocked);
    mockServer.ExpectAndReturn("MapPath", pathToTemporaryConfig);

    // Set up calls for the context
    MockObject mockContext = MockManager.MockObject(typeof(System.Web.HttpContext), Constructor.Mocked);
    mockContext.ExpectGetAlways("Cache", mockCache.Object);
    mockContext.ExpectGetAlways("Server", mockServer.Object);

    // Use natural mocks to ensure the mock context is always returned
    using(RecordExpectations recorder = RecorderManager.StartRecording())
      // Ensure any call for HttpContext always gets returned
      System.Web.HttpContext dummyContext = System.Web.HttpContext.Current;

    System.Collections.ArrayList actual = ConfigService.GetConfiguration();
    Assert.IsNotNull(actual, "The configuration returned should not be null.");
    // ... do other assertions that validate the returned config ...


Using TypeMock, I was able to easily mock a web context and test this code without having to impact the design or the API usability.

It sounds like I’m shilling for TypeMock, and maybe I am a little. On a larger scale, though, I’m just happy I’m able to get full test coverage without sacrificing my usable API.

And if someone reports a defect with this code? Piece of cake to get a mocked test into place that replicates the behavior and even easier to track down the issue because I don’t have all of these additional unnecessary layers of abstraction to fight through. The code is simple - simple to read, simple to understand, and simple to troubleshoot for the next developer who has to try and fix it. You have to love that.

I’m not sure whether it’s out of necessity or boredom that odd office food concoctions get generated. I know I’ve made up some crazy stuff - some good, some not so good. For example, I know you can’t make Coke Blak by just combining coffee and Coke.

I think it’d be interesting to compile a list of office-based recipes - things you can make with the stuff you find around a typical office (coffee, sugar packets, cup-o-noodles, etc.). I found a recipe for chai tea based on office supplies, though I admit I haven’t tried it yet.

Here are a couple of things I’ve made (try at your own risk, YMMV):

FRUIT PUNCH SODA 1 can Sierra Mist (or 7-Up, Sprite, etc.) 1 packet Crystal Light On-The-Go This is reasonably tasty and a good change from the norm if you like this sort of thing and your office only carries standard soda flavors. Pour the can of pop into a cup and pour the packet of Crystal Light into the pop slowly. Stir slowly with a stir stick until thoroughly mixed, pausing when you see the foam start to rise. If you do anything fast, you’re going to have a mess on your hands.

MOCHA 3/4 mug coffee 1/4 mug hot water 1 packet hot chocolate 2 packets creamer Mix contents together thoroughly and enjoy. The water helps to make chocolate a little more prevalent and make it feel less… thick. Depending on your mood or personal taste, you may just go with the full coffee/no water approach.

ICED TEA 1/2 mug hot water 1 black tea bag ice Put the tea packet in the mug of hot water and leave it. Freaking over-steep it. The tea’s got to be strong because once you take the tea bag out, you’ve got to fill the rest of your mug with ice and let the ice melt, and that’s going to water down the tea. You’ll need to experiment with this one based on personal taste - I like really strong iced tea, so I leave the tea bag in for a looooong time.

SWEET TEA Follow the iced tea recipe but add four to six sugar packets to the tea before adding the ice. (If you remember your high school chemistry, solids generally dissolve better in hot liquids.)

If you have a recipe or link to contribute, leave it in the comments. I think it’d be cool to get a big list of office food recipes.

I love my wife dearly, but sometimes she’s just frustrating. For example, when she has some technical problem (say, with the iPod or the computer) that she needs solved, I’ll come in and help her out, but she gets impatient - as I’m trying to explain the answer, she’s already stopped listening. She doesn’t want to hear why the answer is what it is, she just wants to have her immediate problem solved right now thank you very much.

I do my best to be very pleasant during these exchanges, but it’s exasperating, and it takes its toll on my sanity. Take, for instance, a recent exchange I had with one of my co-workers trying to determine why a particular method was returning a value he didn’t expect:

Travis: Okay, so the thing is returning this answer to you because – Michael: I need to know how to get it to return this other thing though. T: Right, but after that, you’re going to need to know how to – M: But how do I get it to return the answer I want? T: Well, you have to do set this parameter this way before you can – M: That sounds really complicated. I’m just trying to do something simple. How do I get it to do what I want? T: I’m getting to that, but there are a few steps that – M: I’m already not listening. What if I just type this in here and hit this button? [He types something in and hits the button.] T: Um, that’s not going to – M: Hey, that’s not working. What about if I try this other thing and hit the button? [Types this other thing in and hits the button.] T: That’s not going to – M: It’s not working. I don’t understand why it’s not working. I just need it to – T: [Exasperated] Look, hon, I can’t tell you how to do what you want to do until you stop and listen to me. M: [Silence] T: I just totally called you “honey,” didn’t I?

Yeah, I did. It was exactly like one of the exchanges I’ve had altogether far too often with my own very loved - but very impatient with technology - wife.

It got really hot in that cubicle for a couple of seconds. And then we laughed our asses off.

process comments edit

I understand that the IT department does their best to help people solve problems and offer services that improve…

You’re asleep already, aren’t you?

Okay, the short version: The company IT department is a good thing, and what they do is meant to help. I’m totally on board with that. I used to be in the IT department. We weren’t out to screw you, I promise.

Here’s the deal, though:

There are a certain quantity of problems that need to be solved, and your computer has a fixed amount of capacity to solve them in. Your disk isn’t getting any faster, your CPU isn’t going to magically process at an extra gigahertz faster. You’ve got what you’ve got. With that, besides getting work done, you probably also want to:

  • Back things up - It’d be a shame if you lost your work.
  • Stop the spread of viruses - You don’t want to get infected, now, do you?
  • Keep company data secure - If someone breaks into your car and steals your laptop while you’re picking up donuts for the morning meeting at the store, they shouldn’t get access to your proprietary data

Each of those things are valuable, but they also eat resources on your system. In some cases, not a trivial amount, either.

I have a 1.7GHz processor and 1.5GB of RAM. It’s not a super powerhouse, but it should be enough to get the job done. It took me eight full minutes to boot up and log in this morning, and my machine is still tanked. Why?

Say you’re getting ready to ride in a bike race. Your computer is your bike. “We got you a pretty decent bike,” says IT, “This bike will definitely get you over the finish line.” You look the bike over, check the specs, and take it out for a spin. Hell, yeah, that bike will do the trick.

“Now, it’s a long race, so you’re going to need some supplies. We’re here to help you,” says IT. And they are - it is a long race, and you do need some supplies. You’re thinking maybe a couple of energy bars and some water.

“First we’re going to strap this 30 gallon drum of water on your back. Yeah, it’s pretty heavy, but you have a fast bike. Oh, and in the event you need to take a break and get off the bike, you don’t want the bike rolling away (?!) so here’s an anvil. It’s okay, though, you have a fast bike. Oh, and we know the first leg of the race is basically a huge hill, but later in the race you’ll want more high gears, so we replaced all of the low gears with high gears so you don’t have to pedal as hard later on. But you have a pretty fast bike, so it should be okay.”

You struggle onto your bike and it barely moves. No one seems to be terribly concerned about this, though, because, crap, man, you have a fast stinking bike so it has to be you causing the issue. And if you complain?

“Yeah, um, I need some water, but I really don’t think I need a drum of water - I only need a water bottle.”

“Well, you can give us back the drum of water, then.”

“Can you give me a water bottle? I do need water, just not a drum.”

“Um, no - all we have is a full drum of water. If you only want a bottle, you’re on your own.”


The cumulative effect of the daily incremental backup, real-time virus scanner, real-time disk encryption, and everything else that runs in the background to help me out is killing me. I booted up this morning and with the startup cost of all of these services, I’m surprised they don’t send someone downstairs to personally punch me in the junk every morning, too. I mean, think about it - whenever I read or write a file, it goes through both a real-time AES 256 encryption and the real-time virus scanner, neither of which I have control to configure because it’s all centrally administered “for us.” I look sideways at this thing and the CPU is pegged and the disk light is on solid for three minutes.

But, hey, if I don’t want to suffer the cost of the incremental backup software (that runs for probably an hour each morning), I can uninstall it. Is there any other backup mechanism? No - if I uninstall it, I’m on my own. Or I could set it to run when I’m not at work… but, oh, wait, you need to be connected to the network to have it run, and the whole point of my having a laptop is so I can take it home with me at night and work disconnected if I need to. There’s no time when I’m not working on my computer that it’s connected to the corporate network. (Not that it matters; I only have about a 30 - 50% success rate on completing a backup at any given point anyway.)

Don’t get me wrong, I appreciate what the IT department does for me and they’re a necessary piece of the puzzle. I just feel a little anchored down lately by all the help, is all, and I don’t think folks consider the overhead of all of these helpful-but-necessary services before rolling them out.

media comments edit

Remember how I just bought a new DVD player because my old one wasn’t playing movies? Now that I think about it, the movies it was having trouble with were Casino Royale and Stranger Than Fiction. The symptoms were basically the same as a bad disc - you put the disc in, the player tries and tries to read it to no avail, and you end up ejecting the disc.

From Slashdot

“It seems that the most recent DVDs released by Sony — specifically Stranger Than Fiction, Casino Royale, and The Pursuit of Happyness — have some kind of ‘feature’ that makes them unplayable on many DVD players. This doesn’t appear to be covered by the major media yet, but this link to a discussion over at Amazon gives a flavor of the problems people are experiencing. A blogger called Sony and was told the problem is with the new copy protection scheme, and they do not intend to fix it. Sony says it’s up to the manufacturers to update their hardware.”

Did I just buy a new DVD player because Sony screwed me?

At least I didn’t buy a new Sony DVD player. It’s a Samsung. :)