如何使用 shims 和/或 Moq 模拟服务器传输方法

我正在尝试模拟以下传输请求。


我尝试过使用 Moq,但它不喜欢它是一个方法调用的事实。有任何想法吗?


我试过使用垫片,但对我来说并不完全有意义。https://social.msdn.microsoft.com/Forums/vstudio/en-US/4e423407-300d-46ba-bfc9-30465fb18f07/how-to-fake-httpcontextcurrent-using-shim?forum=vstest 我尝试模拟http上下文这种方式,但它也没有奏效。 https://justinchmura.com/2014/06/26/mock-httpcontext/


public class MyModule1 : IHttpModule

{

    /// <summary>

    /// You will need to configure this module in the Web.config file of your

    /// web and register it with IIS before being able to use it. For more information

    /// see the following link: https://go.microsoft.com/?linkid=8101007

    /// </summary>

    #region IHttpModule Members


    public void Dispose()

    {

        //clean-up code here.

    }


    public void Init(HttpApplication context)

    {

        // Below is an example of how you can handle LogRequest event and provide 

        // custom logging implementation for it

        context.LogRequest += new EventHandler(OnLogRequest);

        context.BeginRequest += new EventHandler(OnBeginRequest);

    }


    private void OnBeginRequest(object sender, EventArgs e)

    {

        onbegin(new HttpContextWrapper(((HttpApplication)sender).Context));



    }


    private void onbegin(HttpContextBase context)

    {

        // other header stuff goes here

        context.Server.TransferRequest("bobsyouruncle", true);

    }


    #endregion


    public void OnLogRequest(Object source, EventArgs e)

    {

        //custom logging logic can go here

    }


小唯快跑啊
浏览 87回答 1
1回答

FFIVE

类似于我在此处提供的答案中采用的方法如何在 IHttpModules 中测试 HttpApplication 事件您可以创建一个工厂方法/函数,将当前紧密耦合的实现问题包装在抽象中,从而实现更好的模拟和可测试性重构模块public class MyModule1 : IHttpModule {&nbsp; &nbsp; public void Dispose() {&nbsp; &nbsp; &nbsp; &nbsp; //clean-up code here.&nbsp; &nbsp; }&nbsp; &nbsp; public void Init(HttpApplication application) {&nbsp; &nbsp; &nbsp; &nbsp; // Below is an example of how you can handle LogRequest event and provide&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // custom logging implementation for it&nbsp; &nbsp; &nbsp; &nbsp; application.LogRequest += new EventHandler(OnLogRequest);&nbsp; &nbsp; &nbsp; &nbsp; application.BeginRequest += new EventHandler(OnBeginRequest);&nbsp; &nbsp; }&nbsp; &nbsp; public Func<object, HttpContextBase> GetContext = (object sender) => {&nbsp; &nbsp; &nbsp; &nbsp; return new HttpContextWrapper(((HttpApplication)sender).Context);&nbsp; &nbsp; };&nbsp; &nbsp; public void OnBeginRequest(object sender, EventArgs e) {&nbsp; &nbsp; &nbsp; &nbsp; var context = GetContext(sender);&nbsp; &nbsp; &nbsp; &nbsp; onbegin(context);&nbsp; &nbsp; }&nbsp; &nbsp; private void onbegin(HttpContextBase context) {&nbsp; &nbsp; &nbsp; &nbsp; // other header stuff goes here&nbsp; &nbsp; &nbsp; &nbsp; context.Server.TransferRequest("bobsyouruncle", true);&nbsp; &nbsp; }&nbsp; &nbsp; public void OnLogRequest(Object source, EventArgs e) {&nbsp; &nbsp; &nbsp; &nbsp; //custom logging logic can go here&nbsp; &nbsp; }&nbsp; &nbsp; //...}测试GetContext时可以替换工厂函数以使用模拟。例如[TestMethod]public void Server_Should_Transfer() {&nbsp; &nbsp; //Arrange&nbsp; &nbsp; var server = new Mock<HttpServerUtilityBase>();&nbsp; &nbsp; var context = new Mock.<HttpContextBase>();&nbsp; &nbsp; context.Setup(_ => _.Server).Returns(server.Object);&nbsp; &nbsp; var sut = new MyModule1();&nbsp; &nbsp; //replace with mock context for test&nbsp; &nbsp; sut.GetContext = (object sender) => context.Object;&nbsp; &nbsp; //Act&nbsp; &nbsp; sut.OnBeginRequest(new object(), EventArgs.Empty);&nbsp; &nbsp; //Assert&nbsp; &nbsp; server.Verify(_ => _.TransferRequest("bobsyouruncle", true), Times.AtLeastOnce);}
打开App,查看更多内容
随时随地看视频慕课网APP