February 0926

Sharing config files between projects – Unit testing

Ok this is a post where I’m really looking for advice on how other people approach unit testing. I’m relatively new to the whole unit-testing thing so haven’t yet quite wrapped my head around some of its nuances.

I’ve written a Web Service Application that exposes a few simple methods there is also a simple logging class that creates event and error logs and want to write unit tests for these. I decided to start with something simple so thought I’d test the logging methods.

Public Shared Sub WriteEvent(ByVal strWebService As String, ByVal strInputParams As String)
CheckCreateLogPath(_eventLogPath)
Dim sw As New StreamWriter(_eventLogPath & "Event_" & Format(Now, DATE_FORMAT) & ".log", True)
sw.WriteLine(Format(Now, FULL_DATE_FORMAT) & SEPERATOR & strWebService & SEPERATOR & strInputParams)
sw.Close()
End Sub

All the logging method is doing here is taking the values passed to it and writing them to a text file, the location of the file is stored in the web.config so this class has a const to hold that value. However when calling this method in my Unit Test the const is not getting populated so the private helper function that creates the folder if it doesn’t complains that I’m passing a null path. Further when I attempt to use the same way of setting the folder path in my unit test I also get a null – this I assume is because the web.config file is not accessible to my unit test.

<Test()> _
Public Sub CanWriteEvent()
' Arrange
Const TEST_ERROR_STRING As String = "Test Event"
' Act
Logging.WriteEvent(TEST_ERROR_STRING)
' Assert
Assert.Contains(ReadFromFile(_errorLogPath), TEST_ERROR_STRING)
End Sub

So the question is what is the best way of working here. I don’t really want to be hard coding the path to my logging folder in the class, the web.config file seems the proper place for that. Can I share the web.config file with my testing project? If not do I need to set my path in the SetUp method, is it important to use the same folder path? Different folders may have different permissions and I wouldn’t find that out if I’m not testing for that. Is there a more fundamental problem with the way I’m writing either my tests or my classes that’s given rise to this problem and so should I be learning a different approach?

Permalink | Comments (1)

Comments

  1. тексты песен Russia тексты песен (Saturday, March 28, 2009) #

    Thanks for sharing this cool article

Comments are closed