Help Testing Modules Using Rhino Mock

How to make unit test the next class using Rhino Mocks

public interface IXmlTransformer { void Transform(Stream inputXml, Stream transformedXml); } public class XmlToFOTransformer : IXmlTransformer { private string styleSheetPath = string.Empty; private bool fillable = true; public XmlToFOTransformer( string styleSheetUri, bool shouldAllowUserToEditData) { if (string.IsNullOrEmpty(styleSheetUri)) { throw new ArgumentNullException( "styleSheetUri", "styleSheetUri can not be null"); } styleSheetPath = styleSheetUri; fillable = shouldAllowUserToEditData; } public void Transform(Stream inputXml, Stream transformedXml) { if (inputXml == null) { throw new ArgumentNullException( "inputXml", "Input xml can not be null."); } if (transformedXml == null) { throw new ArgumentNullExceptio( "transformedStream", "TransformedStream can not be null."); } XslCompiledTransform transformer = new XslCompiledTransform(); XsltSettings xsltSettings = new XsltSettings(); xsltSettings.EnableDocumentFunction = true; XmlUrlResolver resolver = new XmlUrlResolver(); XmlReaderSettings readerSettings = new XmlReaderSettings(); readerSettings.DtdProcessing = DtdProcessing.Ignore; try { transformer.Load(styleSheetPath, xsltSettings, resolver); } catch (Exception ex) { throw new ApplicationException(string.Format( CultureInfo.InvariantCulture, "Error while loding & compiling the Xsl file, the system returned {0}", ex.Message)); } XmlReader inputXmlReader; try { inputXmlReader = XmlReader.Create(inputXml, readerSettings); } catch (Exception ex) { throw new ApplicationException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Error loading the XML file, the system returned {0}", ex.Message)); } // do the transform try { transformer.Transform( inputXmlReader, xsltArguments, transformedXml); transformedXml.Position = 0; } catch (Exception ex) { throw new ApplicationException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Error in transforming the XML file and XSL file, the system returned {0}", ex.Message)); } } } 
+4
source share
2 answers

I would not use Rhino Mocks to unit test this class. Just create a new test and send the hard-coded XML to the method and MemoryStream. You can validate the data written to the MemoryStream after calling the Transform method.

Perhaps you could explain why you want to use Rhino Mocks to test this method?

+1
source

One of the problems I see is that stream parameters are not abstracted by the interface

 void Transform(Stream inputXml, Stream transformedXml); 

But in any case, RhinoMocks allows you to mock abstract classes using the PartialMock function.

So, the test stub would like: (pseudocode)

 var transformer = new XmlToFOTransformer( styleSheetUri, shouldAllowUserToEditData); // Arrange var inputXmlStreamMock = mockRepository.PartialMock<Stream>(ctor args); var transformedXmlStreamMock = mockRepository.PartialMock<Stream>(ctor args); // setup expectations // ... // Act transformer.Transform(inputXmlStreamMock, transformedXmlStreamMock ); // Assert // asserts here 

See Rhino Mocks Partial Mocks for more information.

+1
source

All Articles