亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 shims 和/或 Moq 模擬服務器傳輸方法

如何使用 shims 和/或 Moq 模擬服務器傳輸方法

C#
小唯快跑啊 2022-07-23 09:43:55
我正在嘗試模擬以下傳輸請求。我嘗試過使用 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    }
查看完整描述

1 回答

?
FFIVE

TA貢獻1797條經驗 獲得超6個贊

類似于我在此處提供的答案中采用的方法


如何在 IHttpModules 中測試 HttpApplication 事件


您可以創建一個工廠方法/函數,將當前緊密耦合的實現問題包裝在抽象中,從而實現更好的模擬和可測試性


重構模塊


public class MyModule1 : IHttpModule {

    public void Dispose() {

        //clean-up code here.

    }


    public void Init(HttpApplication application) {

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

        // custom logging implementation for it

        application.LogRequest += new EventHandler(OnLogRequest);

        application.BeginRequest += new EventHandler(OnBeginRequest);

    }


    public Func<object, HttpContextBase> GetContext = (object sender) => {

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

    };


    public void OnBeginRequest(object sender, EventArgs e) {

        var context = GetContext(sender);

        onbegin(context);

    }


    private void onbegin(HttpContextBase context) {

        // other header stuff goes here

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

    }


    public void OnLogRequest(Object source, EventArgs e) {

        //custom logging logic can go here

    }


    //...

}

測試GetContext時可以替換工廠函數以使用模擬。


例如


[TestMethod]

public void Server_Should_Transfer() {

    //Arrange

    var server = new Mock<HttpServerUtilityBase>();

    var context = new Mock.<HttpContextBase>();

    context.Setup(_ => _.Server).Returns(server.Object);


    var sut = new MyModule1();


    //replace with mock context for test

    sut.GetContext = (object sender) => context.Object;


    //Act

    sut.OnBeginRequest(new object(), EventArgs.Empty);


    //Assert

    server.Verify(_ => _.TransferRequest("bobsyouruncle", true), Times.AtLeastOnce);

}


查看完整回答
反對 回復 2022-07-23
  • 1 回答
  • 0 關注
  • 99 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號