System.IO.Abstractions.Extensions icon indicating copy to clipboard operation
System.IO.Abstractions.Extensions copied to clipboard

Mock XmlReader/StringReader and XslCompiledTransform[.Load]

Open rklec opened this issue 2 years ago • 0 comments

Is your feature request related to a problem? Please describe. I want to mock XSL transformation, implemented via XslCompiledTransform.

var xmlTransformer = new XslCompiledTransform();

xmlTransformer.Load(xslPath);
xmlTransformer.Transform(xmlFilePath, null, output);

So there are two use cases:

  • mock the XSL
  • mock the XML

Describe the solution you'd like Let me mock XslCompiledTransform and read the file mock as usual...

Describe alternatives you've considered The XML can quite easily be read from a stream, however, this needs a mock of XmlReader, which we cannot do with this lib here.

Also loading the XSL from a string requires this or at least both need StringReader and a way more complicated implementation.

All in all the above code can be re-written as such to make it work with the mock, however, this is really complicated/complicates the code just for macking it testable - in contrast to the short and understandable code snippet shown above:

var xmlTransformer = new XslCompiledTransform();

await using var openReadXml = FileSystem.File.OpenRead(xmlFilePath);
await using var openReadXsl = FileSystem.File.OpenRead(xslPath);

var xmlReaderSettings = new XmlReaderSettings
{
    Async = true,
    CloseInput = true
};

using var xmlReader = XmlReader.Create(openReadXml, xmlReaderSettings);
using var xslReader = XmlReader.Create(openReadXsl, xmlReaderSettings);
xmlTransformer.Load(xslReader);
xmlTransformer.Transform(xmlReader, null, output);

xmlReader.Close();
xslReader.Close();

Additional context I am open to any other way for other implementations/changes in how I can mock the XML/XSL transformation/reading properly.


Copied/moved from https://github.com/TestableIO/System.IO.Abstractions/issues/751

rklec avatar Nov 22 '21 15:11 rklec