English 中文(简体)
我怎么能嘲笑"回应"呢 与莫克的状态守则?
原标题:How can I mock the Response.StatusCode with Moq?

我有以下方法:

public void SetHttpStatusCode(HttpStatusCode httpStatusCode)
{
    Response.StatusCode = (int)httpStatusCode;
}

和以下试验:

[TestMethod]
public void SetHttpStatusCode_SetsCorrectStatusCode()
{
    //Arrange
    //Any url will suffice
    var mockHttpContext = TestHelpers.MakeHttpContext(""); 
    mockHttpContext.SetupSet(x => x.Response.StatusCode = It.IsAny<int>());

    //creates an instance of an asp.net mvc controller
    var controller = new AppController()
    {
        ControllerContext = new ControllerContext() 
        { 
            HttpContext = mockHttpContext.Object 
        }
    };

    // Act
    controller.SetHttpStatusCode(HttpStatusCode.OK);

    //Assert
    mockHttpContext.VerifySet(x => x.Response.StatusCode = It.IsAny<int>());
}

这里是 makeHttpcontext

public static Mock<HttpContextBase> MakeHttpContext(string url)
{
    var mockHttpContext = new Mock<HttpContextBase>();
    var mockRequest = new Mock<HttpRequestBase>();
    var mockResponse = new Mock<HttpResponseBase>();
    var mockSession = new Mock<HttpSessionStateBase>();

    //request
    mockRequest.Setup(x => x.AppRelativeCurrentExecutionFilePath).Returns(url);
    mockHttpContext.Setup(x => x.Request).Returns(mockRequest.Object);

    //response
    mockResponse.Setup(x => x.ApplyAppPathModifier(It.IsAny<string>())).Returns<string>(x => x);
    mockHttpContext.Setup(x => x.Response).Returns(mockResponse.Object);

    //session
    mockHttpContext.Setup(x => x.Session).Returns(mockSession.Object);

    return mockHttpContext;
}

当我做测试时,我有以下例外:

Test method PA.Tests.Controllers.AppControllerTest.SetHttpStatusCode_SetsCorrectStatusCode
threw exception: 
  

  Moq.MockException: 
    Expected invocation on the mock at least once, 
    but was never performed: x => x.StatusCode = It.IsAny<Int32>()
    
    Configured setups:
    x => x.StatusCode = It.IsAny<Int32>(), Times.Never
    No invocations performed.

How does Moq expect/require invocations to be called? I ve debugged the SetHTTPStatusCode method, the response object is indeed a mocked object, however Moq insists that there was no invocation. Am I missing something? tha

最佳回答

您没有显示您的 < code> 测试帮助者> 的方法。 MakeHttpcontext 方法会让您很难理解到底发生了什么 。

试试这个:

// Arrange
var mockHttpContext = new Mock<HttpContextBase>();
var response = new Mock<HttpResponseBase>();
mockHttpContext.SetupGet(x => x.Response).Returns(response.Object);

//creates an instance of an asp.net mvc controller
var controller = new AppController()
{
    ControllerContext = new ControllerContext() 
    { 
        HttpContext = mockHttpContext.Object 
    }
};

// Act
controller.SetHttpStatusCode(HttpStatusCode.OK);

//Assert
response.VerifySet(x => x.StatusCode = (int)HttpStatusCode.OK);
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签