gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:09:19 25 4
gpt4 key购买 nike

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

最佳答案

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

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

关于unit-testing - 帮助使用 Rhino Mock 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7325427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com