Test Driven Development and objects like the Application class



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

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?



Peter Morris

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.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question