IF I am testing something related to an error, I might want to see the message or stack trace in the console initially. After I'm satisfied with the test I typically do not want to have the console cluttered with anything that might help quickly spot and diagnose a failing test. When refactoring though, it is sometimes useful to once again output additional info. So I have lots of lines scattered about that I comment / uncomment, like:
// System.Diagnostics.Debug.WriteLine(msg);
What is a cleaner way to do this?
Cheers,
Berryl
=== EDIT
Here is an example of what I mean, updated to use a Log service as per Josh's suggestion here.
As a unit test, I want to assert that an error will be displayed to the user, in this case because there is a duplicate name for a new department. I can and do automate that the message is being generated with the correct content.
The message also has to pass a basic smell test that can't be automated though - is it a well structured sentence that most users will quickly understand and know how to fix? I don't want to have to fire up the ui and get a user to make sure it isn't outright silly, so I want to eyeball it by printing it out. And as I alluded to earlier, this is just noise except for the few times the message changes.
[Test]
public void WhenThePropertyIsChanged_IfDuplicateIsFound_DuplicateNameIsPartOfBrokenRuleMessage()
{
const string newName = "Blah";
// force a duplicate
_dao.Stub(x => x.FindByName(newName)).Return(new Department(newName));
var vm = _masterVm.Departments.Last();
vm.Name = newName;
var msg = vm.GetBrokenRules().First().Description;
Log.Service.WriteLine(msg); <=== print it
Assert.That(msg, Is.StringContaining(newName));
Assert.That(vm.BrokenRules.First().Description, Is.EqualTo(msg));
}