5

I am producing an XML file in my unit test using

Public Sub rssParserTest
   Dim Const rssUri as String = "rssTestFile.xml"
   Dim xmlFile = <rss version="2.0">
   ...
                 </rss>
   xmlFile.save(rssUri)

  rssParser(rssUri)
End Sub

and consuming the uri with an XMLTextReader

Public Sub rssParser(ByVal rssUri as string)
    Dim rssXml = New XmlTextReader(rssUri)
    rssXml.read
    ...
End Sub

I want to remove the unit test dependency on a physical file and use a stream instead but my efforts so far have come to nought. (Is this best practise?)

I am using NMock2 for mocking if I should be doing something with that.

Bender
  • 1,688
  • 2
  • 14
  • 20

2 Answers2

10

Rather than force an XmlTextReader via a stream, if you just nead an XmlReader you can just use XNode.CreateReader. That's a much simpler approach than saving to a stream, unless your API forces you to use a stream or an XmlTextReader.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

xmlFile is an XDocument, which can be saved into a MemoryStream, see the following SO question for details:

You can then make your method accept a generic Stream, which can then be a MemoryStream (in the unit test) or a FileStream.

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – kiritsuku Nov 14 '12 at 22:23
  • @sschaef: Are you sure? He asks how he can convert his `xmlFile` variable into a stream without having to write it to disk (see also the question title), and the linked SO question answers that question. Granted, it's not a very *good* answer (I was probably in a hurry back then; it could be extended by a more detailed explanation and a summary of the relevant parts of the linked question), but it does answer the question. Do you still disagree? – Heinzi Nov 15 '12 at 06:11
  • 1
    Ok, very strictly speaking it does provide an answer. But firstly, the linked question is about C#, which means that he solution can not directly moved to VB.NET. Secondly, even than it is a link only answer. In my opinion this makes it more a comment for a hint on how to solve the question. – kiritsuku Nov 15 '12 at 09:54
  • @sschaef: I've added some context. – Heinzi Nov 15 '12 at 10:48