2011-09-06 68 views
4

我如何用單位犀牛製品幫助做用犀牛模擬

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)); 
      } 
     } 
    } 

回答

1

我不會用犀牛製品進行單元測試類測試下面的類單元測試。只需創建一個新的測試,並將一些硬編碼的xml發送到方法和MemoryStream。調用Transform方法後,您可以對寫入MemoryStream的數據進行斷言。

也許你可以解釋爲什麼你想用Rhino Mocks來測試該方法?

+0

好吧,我們wehere使用犀牛製品隨處可見這就是爲什麼只想成爲洽 – user686732

+1

我不建議你使用其他一些技術來編寫你的測試。我通常不會嘲笑對.NET類型的引用,除非他們訪問光盤,網絡,數據庫或其他不適用於單元測試的內容。但它可能是一個味道:)與您的測試祝你好運。 – ThomasArdal

1

的一個問題,我可以看到的是,流paremeters不由接口

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

+0

你知道在哪裏可以找到使用最新框架的Rhino Mocks的優秀articals嗎? – user686732

+0

@ user686732:不幸的是,只有純粹的在線Rhino WIKI ..​​....和像SO這樣的intrawebs論壇;) – sll