Coding and Dismantling Stuff

Don't thank me, it's what I do.

About the author

Russell is a .Net developer based in Lancashire in the UK.  His day job is as a C# developer for the UK's largest online white-goods retailer, DRL Limited.

His weekend job entails alternately demolishing and constructing various bits of his home, much to the distress of his fiance Kelly, 3-year-old daughter Amelie, and menagerie of pets.

TextBox

  1. Fix dodgy keywords Google is scraping from my blog
  2. Complete migration of NHaml from Google Code to GitHub
  3. ReTelnet Mock Telnet Server à la Jetty
  4. Learn to use Git
  5. Complete beta release FHEMDotNet
  6. Publish FHEMDotNet on Google Code
  7. Learn NancyFX library
  8. Pull RussPAll/NHaml into NHaml/NHaml
  9. Open Source Blackberry Twitter app
  10. Other stuff

Unit Testing Good Patterns - Stop Putting String Literals in Code!

Hi all,

A short one - and it's actually a tip I've picked up from a co-ninja in the office (much kudos due to @datoon83). When doing some work with unit tests, we've discovered an anti-pattern we've applied throughout our application, which is to hard-code messages into our source code.

The simple lesson - don't use string literals in your application codeuse Resource files instead! There are plenty of examples where we've done it, things like exception descriptions, audit  notes, user confirmation messages, etc.

The problem was highlighted beautifully when we were asserting against classes in legacy code that generated and saved away audit notes.  We wanted to assert that the note was well formed with the correct identifiers and timestamps, so out comes Moq, and we simply dubugged the code, copyied the value we saw being passed into the mocked object, and pasted this into our test to safeguard it.

But lo and behold, someone comes along and fixes a spelling mistake, or re-formats a message, and even though our code still works fine, our tests are all breaking. After moving to Resource files, no more copy-and-pasting, no more brittle tests that need fixing every time someone re-words a user message, and for free we get internationalisation support.  Awesomeness!

Russ


Permalink | Comments (0)

Pingbacks and trackbacks (1)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading