使用Rhino Mock帮助进行单元测试

时间:2011-09-06 19:57:21

标签: unit-testing rhino-mocks

如何使用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));
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

我不会使用Rhino Mocks对该类进行单元测试。只需创建一个新测试并将一些硬编码的xml发送到方法和MemoryStream。在调用Transform-method之后,您可以对写入MemoryStream的数据进行断言。

也许您可以解释为什么要使用Rhino Mocks来测试该方法?

答案 1 :(得分:1)

我可以看到的一个问题是流参数不是通过接口抽象的

void Transform(Stream inputXml, Stream transformedXml);

但无论如何,RhinoMocks允许使用PartialMock功能来模拟抽象类。

所以测试存根需要:(伪代码)

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

有关详情,请参阅:Rhino Mocks Partial Mocks