Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7990557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:03:06+00:00 2026-06-04T13:03:06+00:00

I have the following method: public void SetHttpStatusCode(HttpStatusCode httpStatusCode) { Response.StatusCode = (int)httpStatusCode; }

  • 0

I have the following method:

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

And the following test:

    [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>());
    }

Also, Here is 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;
    }

When I run the test, I get the following exception:

    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?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:03:09+00:00Added an answer on June 4, 2026 at 1:03 pm

    You haven’t shown what your TestHelpers.MakeHttpContext method does so it’s a bit difficult to understand what’s going on.

    Try like this:

    // 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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method in a Java app: public void setPixel(int x, int
I have the following test case [TestMethod()] [DeploymentItem(Courses.sdf)] public void RemoveCourseConfirmedTest() { CoursesController_Accessor target
I have the following method: public void PutFile(string ID, Stream content) { try {
I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {
I have an interface (called Subject ) that has the following method: public void
Hey - I have the following method on my UITableView controller public void RemoveEventFromList(Event
If i have the following method - public static void C() { Connection con
I have the following extension method: public static void ThrowIfArgumentIsNull<T>(this T value, string argument)
I have the following method signature: public static void InvokeInFuture(Delegate method, params object[] args)
I have a database with the following method: public void deleteNote(long rowId) { mDb.delete(DATABASE_TABLE,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.