Test Driven Development and objects like the Application class

  • Thread starter Thread starter joecool1969
  • Start date Start date
J

joecool1969

Where I work, TPTB have decided that all new projects we work on
(using C#.NET with VS2008 and Team Foundation), shall implement a unit
test project. Something that I have been working on lately writes an
error log (if there were any errors) to a log file in a folder in the
Application Data special folder, like:

c:\documents and settings\myusername\Application Data\mycompanyname
\applicationname

The path to this folder is generated in a public method that is used
to initialize the application and uses the Environment class to get
the special folder path and the Application class to get the company
name and application name.

Now I would like to write a unit test for this method, but the
Application class is different when the application runs under the
context of a unit test. So the test fails.

What is the "best practice" for "test drive development" that will
produce testable code in this case?
 
Dependency injection.

You can pass in an interface which has a LogPath property which during
testing you can pass in a different path. You could even create an
IFileSystemService which has methods such as

Stream CreateNewFile(string path);

So you could even pass in a mock one of these which returns a memory stream.
 
Back
Top