开发者

How can I unit test the modification of an XML file which recursively points to other XML files?

I am attempting to unit test the modification of an XML structure like:

<element1>
    <open file="otherfile.xml">
</element1>

Suppose that otherfile.xml contains:

<element2>
    <open file="anotherfile.xml">
    <happytag/>
</element2>

And that anotherfile.xml looks like:

<element3>
    <sadtag/>
</element3>

The correct modification copies and pastes code from the file to be opened into the code that called the open tag. Therefore, I am expecting a structure like:

<element1>
    <element2>
        <element3>
            <sadtag/>
        </element3>
        <happytag/>
    </elem开发者_如何学Goent2>
</element1>

The problem I am having is that I am unsure how to go about unit testing the creation of this structure. Ideally, and what I have tried to far (unsuccessfully), is to create this unit test so that I do not need to make any calls to the file system in order to create the structure. To do that, I create a mock object which pretends to be an input stream which opens a file, but is really just returning a stream created from the text of

<element1>
    <open file="otherfile.xml">
</element1> 

The problem I have in this approach is that I cannot figure out a way to inject the text of otherfile.xml so that a mock object will return the text of:

<element2>
    <open file="anotherfile.xml">
    <happytag/>
</element2>

Another thing I've tried is to put the text of anotherfile.xml into open file="", like open file="&lt;element3&gt;...&lt;/element3&gt;">. This doesn't work because the XML parser complains that it isn't valid XML.

I could also create these xml files from strings before I run the test and then delete them after the test.


Create a file accessor interface and then providing two implementations. One implementation actually opens items on the filesystem. The other implementation accesses data stored in a map inside the object.

So your testing object would be constructed like this:

file_data = {
    "otherfile.xml": "<element2>\n    <open file=\"anotherfile.xml\">\n    <happytag/>\n</element2>",
    "anotherfile.xml": "<element3>\n    <sadtag/>\n</element3>"
}
accessor = TestFileAccessor(file_data)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜