2015-05-06 68 views
0

我有基於其他依賴的結果操縱依賴類:你怎麼測試的依賴互動

public class PdfPagePresenter : IPdfPagePresenter 
    { 
     private readonly IPdfPageView _view; 
     private readonly IPdfPageVisitor<bool> _querystringvalidator; 
     private readonly IPdfPageVisitor<bool> _servicepackagevalidator; 
     private readonly IPdfPageVisitor<byte[]> _watermarkgenerator; 

     public PdfPagePresenter(IPdfPageView view, IPdfPageVisitorAbstractFactory visitorAbstractFactory) 
     { 
      _view = view; 
      _querystringvalidator = visitorAbstractFactory.QueryStringValidatorFactory().Invoke(); 
      _servicepackagevalidator = visitorAbstractFactory.ServicePackageValidatorFactory().Invoke(); 
      _watermarkgenerator = visitorAbstractFactory.WatermarkStreamGeneratorFactory().Invoke(); 
     } 

     public void OnPageLoad() 
     { 

      if (!_view.Accept(_querystringvalidator)) 
      { 
       _view.RespondRedirect("Somepage.aspx"); 
       return; 
      } 

      if (!_view.Accept(_servicepackagevalidator)) 
      { 
       _view.RespondWrite("Cannot find the service package in our system."); 
       return; 
      } 

      _view.RespondBinaryWrite(_view.Accept(_watermarkgenerator), "application/pdf"); 
     } 
    } 

我想我可以假人驗證工廠和字節工廠創建一個迴應,但我怎麼考調用依賴項中的方法。這是一個編排類型類。

+0

所以,你要驗證的onPageLoad被調用? – Darek

+0

我想測試多個案例,其中每個驗證執行正確的操作 –

+0

如果querystringvalidator返回false我想知道視圖重定向被調用,如果servicepackagevalidator返回false視圖寫入被調用,否則binar寫入被調用 –

回答

0

使用起訂量和NUnit

[TestFixture] 
public class TestPdfPagePresenter 
{ 
    [Test] 
    public void TestOnPageLoadInvalidQuerystringRunsRedirect() 
    { 
     var qstringval = new Mock<IPdfPageVisitor<bool>>(); 
     var spackageval = new Mock<IPdfPageVisitor<bool>>(); 
     var watermarkgen = new Mock<IPdfPageVisitor<byte[]>>(); 

     var fac = new Mock<IPdfPageVisitorAbstractFactory>(); 
     fac.Setup(f => f.QueryStringValidatorFactory()).Returns(() => qstringval.Object); 
     fac.Setup(f => f.ServicePackageValidatorFactory()).Returns(() => spackageval.Object); 
     fac.Setup(f => f.WatermarkStreamGeneratorFactory()).Returns(() => watermarkgen.Object); 


     var view = new Mock<IPdfPageView>(); 
     view.Setup(v => v.Accept(qstringval.Object)).Returns(false); 
     view.Setup(v => v.Accept(spackageval.Object)).Returns(true); 
     view.Setup(v => v.Accept(watermarkgen.Object)).Returns(new byte[0]); 

     Assert.DoesNotThrow(() => 
     { 
      var presenter = new PdfPagePresenter(view.Object, fac.Object); 
      presenter.OnPageLoad(); 
      view.Verify(v => v.RespondRedirect("Somepage.aspx")); 
      view.Verify(v => v.RespondWrite("Cannot find the service package in our system."), Times.Never); 
      view.Verify(v => v.RespondBinaryWrite(watermarkgen.Object.Result(), "application/pdf"), Times.Never); 
     }); 
    } 

    [Test] 
    public void TestOnPageLoadInvalidServicePackageRunsWrite() 
    { 
     var qstringval = new Mock<IPdfPageVisitor<bool>>(); 
     var spackageval = new Mock<IPdfPageVisitor<bool>>(); 
     var watermarkgen = new Mock<IPdfPageVisitor<byte[]>>(); 

     var fac = new Mock<IPdfPageVisitorAbstractFactory>(); 
     fac.Setup(f => f.QueryStringValidatorFactory()).Returns(() => qstringval.Object); 
     fac.Setup(f => f.ServicePackageValidatorFactory()).Returns(() => spackageval.Object); 
     fac.Setup(f => f.WatermarkStreamGeneratorFactory()).Returns(() => watermarkgen.Object); 


     var view = new Mock<IPdfPageView>(); 
     view.Setup(v => v.Accept(qstringval.Object)).Returns(true); 
     view.Setup(v => v.Accept(spackageval.Object)).Returns(false); 
     view.Setup(v => v.Accept(watermarkgen.Object)).Returns(new byte[0]); 

     Assert.DoesNotThrow(() => 
     { 
      var presenter = new PdfPagePresenter(view.Object, fac.Object); 
      presenter.OnPageLoad(); 
      view.Verify(v => v.RespondRedirect("Somepage.aspx"), Times.Never); 
      view.Verify(v => v.RespondWrite("Cannot find the service package in our system.")); 
      view.Verify(v => v.RespondBinaryWrite(watermarkgen.Object.Result(), "application/pdf"), Times.Never); 
     }); 
    } 

    [Test] 
    public void TestOnPageLoadValidRunsBinaryWrite() 
    { 
     var qstringval = new Mock<IPdfPageVisitor<bool>>(); 
     var spackageval = new Mock<IPdfPageVisitor<bool>>(); 
     var watermarkgen = new Mock<IPdfPageVisitor<byte[]>>(); 

     var fac = new Mock<IPdfPageVisitorAbstractFactory>(); 
     fac.Setup(f => f.QueryStringValidatorFactory()).Returns(() => qstringval.Object); 
     fac.Setup(f => f.ServicePackageValidatorFactory()).Returns(() => spackageval.Object); 
     fac.Setup(f => f.WatermarkStreamGeneratorFactory()).Returns(() => watermarkgen.Object); 

     var returnbytes = new byte[0]; 
     var view = new Mock<IPdfPageView>(); 
     view.Setup(v => v.Accept(qstringval.Object)).Returns(true); 
     view.Setup(v => v.Accept(spackageval.Object)).Returns(true); 
     view.Setup(v => v.Accept(watermarkgen.Object)).Returns(returnbytes); 

     Assert.DoesNotThrow(() => 
     { 
      var presenter = new PdfPagePresenter(view.Object, fac.Object); 
      presenter.OnPageLoad(); 
      view.Verify(v => v.RespondRedirect("Somepage.aspx"), Times.Never); 
      view.Verify(v => v.RespondWrite("Cannot find the service package in our system."), Times.Never); 
      view.Verify(v => v.RespondBinaryWrite(returnbytes, "application/pdf")); 
     }); 
    } 

}