I'm trying to get into unit testing and the TDD way to do something, but I ran into a problem, I'm not sure what to do with it.
I have a collection that saves itself to disk using XDocument and XmlWriter. I know that you should not write the file to disk, and then check it so that I get the output of XmlWriter in the memory stream, and then check the contents of the memory stream. The function is as follows:
public void Save() { using (XmlWriter xmlWriter = XmlWriter.Create(m_StreamProvider.SaveFileStream(m_FilenameProvider.Filename))) { XDocument xDoc = new XDocument(new XElement("BookmarkCollection", Items.Select(bookmark => new XElement("Bookmark", new XElement("Name", bookmark.Name), new XElement("Link", bookmark.Link), new XElement("Remarks", bookmark.Remarks), new XElement("DateAdded", bookmark.DateAdded), new XElement("DateLastAccessed", bookmark.DateLastAccessed)) ) )); xDoc.Save(xmlWriter); } }
And unit test is
[Test] public void Save_OneItemCollection_XmlCreatedCorrectly() {
The statement here is not too fragile (I know I can use String.Compare (), but it would have similar problems.) Am I testing correctly? Am I mocking what happened?
The whole entrance is much appreciated!
user1333
source share