我有一个类根据其他依赖结果来操作依赖项:
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 :(得分:1)
使用Moq和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"));
});
}
}