Nunit 在自定义NancyFx引导程序中测试代码
我有一个自定义的Nancy引导程序,它使用StructureManancyBootstrapper,但不管容器是什么,问题都是一样的Nunit 在自定义NancyFx引导程序中测试代码,nunit,moq,nancy,structuremap3,Nunit,Moq,Nancy,Structuremap3,我有一个自定义的Nancy引导程序,它使用StructureManancyBootstrapper,但不管容器是什么,问题都是一样的 public class CustomNancyBootstrapper : StructureMapNancyBootstrapper { protected override void RequestStartup(IContainer container, IPipelines pipelines, NancyContext context)
public class CustomNancyBootstrapper : StructureMapNancyBootstrapper
{
protected override void RequestStartup(IContainer container, IPipelines pipelines, NancyContext context)
{
var auth = container.GetInstance<ICustomAuth>();
auth.Authenticate(context);
}
}
有没有一种“适当的”/“正式的”方法可以做到这一点,而不必创建另一个派生类并公开方法,因为这看起来像是一种黑客行为
谢谢我想您可以通过使用以下地址“伪造”请求:
关于测试NancyFx应用程序,有很多文章:
原来Nancy提供了一个iRequestStartup接口,因此您可以将代码从自定义引导程序中取出,并执行如下操作
[Test]
public void RequestStartup_Calls_CustomAuth_Authenticate_WithContext()
{
// set up
var mockAuthentication = new Mock<ICustomAuth>();
var mockContainer = new Mock<IContainer>();
var mockPipelines = new Mock<IPipelines>();
var context = new NancyContext();
mockContainer.Setup(x => x.GetInstance<ICustomAuth>()).Returns(mockAuthentication.Object);
// exercise
_bootstrapper.RequestStartup(_mockContainer.Object, _mockPipelines.Object, context);
// verify
mockAuthentication.Verify(x => x.Authenticate(context), Times.Once);
}
public class MyRequestStart : IRequestStartup
{
private readonly ICustomAuth _customAuthentication;
public MyRequestStart(ICustomAuth customAuthentication)
{
if (customAuthentication == null)
{
throw new ArgumentNullException(nameof(customAuthentication));
}
_customAuthentication = customAuthentication;
}
public void Initialize(IPipelines pipelines, NancyContext context)
{
_customAuthentication.Authenticate(context);
}
}
测试简单明了
[Test]
public void When_Initialize_Calls_CustomAuth_Authenticate_WithContext()
{
// set up
var mockAuth = new Mock<ICustomAuth>();
var requestStartup = new MyRequestStart(mockAuth.Object);
var mockPipeline = new Mock<IPipelines>();
var context = new NancyContext();
// exercise
requestStartup.Initialize(mockPipeline.Object, context);
// verify
mockAuth.Verify(x => x.Authenticate(context), Times.Once);
}
[测试]
当\u Initialize\u调用\u CustomAuth\u Authenticate\u with context()时公共无效
{
//设立
var mockAuth=new Mock();
var requestStartup=newmyrequeststart(mockAuth.Object);
var mockPipeline=new Mock();
var context=new NancyContext();
//练习
初始化(mockPipeline.Object,context);
//核实
mockAuth.Verify(x=>x.Authenticate(上下文),次.Once);
}
感谢您的回复。我看到了这一点,但觉得有点做作,因为它实际上是在测试路线,只是间接测试引导程序。。还是。。我认为这是最好的办法,所以我会记下答案。是的,我知道。但是他们在GitHub上有一个非常积极的开发,我建议您再次提出这个问题,谢谢Anton,我提出了这个问题,它也有需要通过上述两种方法进行测试的实际代码
public class MyRequestStart : IRequestStartup
{
private readonly ICustomAuth _customAuthentication;
public MyRequestStart(ICustomAuth customAuthentication)
{
if (customAuthentication == null)
{
throw new ArgumentNullException(nameof(customAuthentication));
}
_customAuthentication = customAuthentication;
}
public void Initialize(IPipelines pipelines, NancyContext context)
{
_customAuthentication.Authenticate(context);
}
}
[Test]
public void When_Initialize_Calls_CustomAuth_Authenticate_WithContext()
{
// set up
var mockAuth = new Mock<ICustomAuth>();
var requestStartup = new MyRequestStart(mockAuth.Object);
var mockPipeline = new Mock<IPipelines>();
var context = new NancyContext();
// exercise
requestStartup.Initialize(mockPipeline.Object, context);
// verify
mockAuth.Verify(x => x.Authenticate(context), Times.Once);
}