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 900019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:19:02+00:00 2026-05-15T15:19:02+00:00

Yesterday I re-factored the below method to return either a full view or a

  • 0

Yesterday I re-factored the below method to return either a full view or a partial view.

public ActionResult List(int page)
{
    var viewModel = GetListViewModel(page);

    if(Request.IsAjaxRequest())
    {
        return PartialView("_list", viewModel);
    }
     return View("PartsList", viewModel);
}

But now my tests have broken, they are failing on the if statement. I’ve done a google and found you can mock/stub the HTTP Requests with something like,

HttpContextBase mockedContext = MockRepository.GeneratePartialMock<HttpRequestBase>();
HttpRequestBase mockedContext = MockRepository.GeneratePartialMock<HttpContextBase>();

mockedContext.Stub(x => x.Request).Return(mockedRequest);
mockedRequest.Stub(r => r["X-Requested-With"]).Return("");

subject.ControllerContext = new ControllerContext { HttpContext = mockedContext };

I implemented the above in my test and it is still falling over.

TEST

public class when_asked_for_the_parts_list_view : context_for_part_controller
{
    static ActionResult _result;
    static IPagination<Part> _parts;
    static PartListPageViewModel _partListPageViewModel;

    Establish context = () =>
    {
         mockedContext.Stub(x => x.Request).Return(mockedRequest);
         mockedRequest.Stub(r => r["X-Requested-With"]).Return("XMLHttpRequest");
         subject.ControllerContext = new ControllerContext { HttpContext = mockedContext };

         _parts = new List<Part>().AsPagination(1);
         _partListPageViewModel = new PartListPageViewModel();

         _partTasks.Stub(x => x.GetParts(1)).Return(_parts);
         _listMapper.Stub(x => x.MapFrom(_parts)).Return(_partListPageViewModel);
    };

    Because of = () =>
    {
        _result = subject.List(1);
    };

    It should_retreve_the_parts =
        () => _partTasks.AssertWasCalled(x=>x.GetParts(1));

    It should_map_the_parts_to_a_viewModel =
        () => _listMapper.AssertWasCalled(x => x.MapFrom(_parts));

    It should_return_the_list_as_a_partialview =
        () => _result.ShouldBeAPartialView().ViewData.Model.ShouldEqual(_partListPageViewModel);
}

ERROR

should map the parts to a viewModel : Failed
The method or operation is not implemented.

System.NotImplementedException: The method or operation is not implemented.

at System.Web.HttpRequestBase.get_Headers()
at HttpRequestBaseProxy5a253d42dd57477f936e24736e848cbb.get_Headers_callback_47()
at HttpRequestBaseProxy5a253d42dd57477f936e24736e848cbb.Invocationget_Headers_66.InvokeMethodOnTarget()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Rhino.Mocks.Impl.ReplayPartialMockState.DoMethodCall(IInvocation invocation, MethodInfo method, Object[] args)
at Rhino.Mocks.Impl.ReplayMockState.MethodCall(IInvocation invocation, MethodInfo method, Object[] args)
at Rhino.Mocks.MockRepository.MethodCall(IInvocation invocation, Object proxy, MethodInfo method, Object[] args)
at Rhino.Mocks.Impl.RhinoInterceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at HttpRequestBaseProxy5a253d42dd57477f936e24736e848cbb.get_Headers()
at System.Web.Mvc.AjaxRequestExtensions.IsAjaxRequest(HttpRequestBase request)
at Catalogue.Web.Controllers.Part.PartController.List(Int32 page) in PartController.cs: line 143
at Catalogue.MSpecTests.UI.Parts.when_asked_for_the_parts_list_view.<.ctor>b__6() in PartControllerTests.cs: line 214

How do I get the request in the Request.IsAjaxRequest() in my controller to pass as an ajax request???

Regards
Rich

Edit – Found this post, my ajax test is now passing but my non-ajax is still failing.

  • 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-05-15T15:19:03+00:00Added an answer on May 15, 2026 at 3:19 pm

    You should probably create an interface like this, which is easy to implement and mock:

    public interface IRequestInfo
    {
        bool IsAjaxRequest { get; }
    }
    

    Then your class might look like this:

    public class MyClass
    {
        private readonly IRequestInfo _request;
    
        public MyClass(IRequestInfo request)
        {
            _request = request;
        }
    
        public ActionResult List(int page)
        {
            var viewModel = GetListViewModel(page);
            if (_request.IsAjaxRequest)
            {
                return PartialView("_list", viewModel);
            }
            return View("PartsList", viewModel);
        }
    }
    

    And your tests becomes:

    [Test]
    public void List_returns_PartialView_for_Ajax_request()
    { 
       // arrange system under test and stubs
       var request = MockRepository.GenerateStub<IRequestInfo>();
       request.Stub(x => x.IsAjaxRequest).Returns(true);
       var myObject = new MyClass(request);
    
       // act
       object result = myObject.List(0);
    
       // assert
       Assert.IsTrue(result is PartialView);
    }
    

    Note how your test is no longer concerned with HTTP headers. Those are an internal concern of the IRequestInfo implementation. And since that implementation will just be a thin wrapper around the ASP.NET HttpContext.Request object, there is no need to unit test it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yesterday I found this function: function clone(obj) { return typeof obj === 'undefined' ?
Yesterday I wanted to add a boolean field to an Oracle table. However, there
Yesterday, I asked this question and never really got an answer I was really
Yesterday, I discovered the Subversion (SVN) blame feature, and I was wondering, is this
Yesterday I was having a heated debate with a coworker on what would be
Yesterday, I found myself writing code like this: SomeStruct getSomeStruct() { SomeStruct input; cin
Yesterday I thought it would be nice to implement my own Trigger in a
Yesterday, I have a discussion with my colleagues about HTTP. It is asked why
Yesterday I got a call from a Microsoft representative asking if we supply hosted
yesterday you had taught me to query record history within a time range here

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.