title: Unit testing a function that outputs via XmlWriter?
body: <p>I'm trying to get into the unit testing and <a href="http://en.wikipedia.org/wiki/Test-driven_development" rel="nofollow noreferrer">TDD</a> way of doing things but I've come across a problem I'm not sure what to do with.</p>
  
  <p>I have a collection that saves itself to disk using XDocument and XmlWriter.  I know you shouldn't write the file to disk then check it so I had the XmlWriter output to a memory stream then I checked the contents of the memory stream.  The function looks like this:</p>
  
  <pre><code>   public void Save()
      {
          using (XmlWriter xmlWriter = XmlWriter.Create(m_StreamProvider.SaveFileStream(m_FilenameProvider.Filename)))
          {
              XDocument xDoc = new XDocument(new XElement("BookmarkCollection",
                      Items.Select(bookmark =&gt; 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);
          }
  
      }
  </code></pre>
  
  <p>And the unit test is</p>
  
  <pre><code>[Test]
      public void Save_OneItemCollection_XmlCreatedCorrectly()
      {
          //Arrange
          MemoryStreamProvider streamProvider = new MemoryStreamProvider();
          IBookmarkCollection collection = XBookmarkTestHelpers.GetXBookmarkCollection(streamProvider);
  
          IBookmark bookmarkToAdd = XBookmarkTestHelpers.GetIBookmark("myLink");
          collection.Add(bookmarkToAdd);
  
          //Act
          collection.Save();
  
          //Assert
          streamProvider.WriteStrean.Position = 0;
          String generatedXml = Encoding.Default.GetString(streamProvider.WriteStrean.GetBuffer());
  
          Assert.IsTrue(String.Equals(generatedXml, m_ExpectedOneItemString), "XML does not match");
      }
  </code></pre>
  
  <p>The assert here fails too fragile (I know I could use String.Compare() but it would have similar issues.),  Am I testing the right thing?  Am I mocking the wrong thing?</p>
  
  <p>All input greatly appreciated!</p>