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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:46:43+00:00 2026-06-05T10:46:43+00:00

I am trying to test an ActionResult that returns a partial view. This action

  • 0

I am trying to test an ActionResult that returns a partial view. This action checks to see if the user is using IE7 or IE8 and will return a different partial if the user is using IE7 or 8. This is because the java-script we are using doesn’t quite work with 7 or 8 so we will treat it differently.

I have left out the model creation because I want to just focus on the Request.Browser.Type.

public ActionResult ActionName(string listing)
{
    if (model.Count > 1 && Request.Browser.Type != "IE8" && Request.Browser.Type != "IE7")
    {
        return PartialView("~/Areas/Features/Views/Video/MultiVideo.cshtml", model);
    }
    return PartialView("~/Areas/Features/Views/Video/SingleVideo.cshtml", model.FirstOrDefault());
}

Ultimately I want to know two things

  1. How can you Mock the Request.Browser.Type if at all?
  2. Is this even good practice to do inside your controller? If not what solutions would be better?

What I have tried:

var browser = new Mock<HttpContext>(MockBehavior.Strict);
browser.Setup(x => x.Request.Browser).Returns("IE9");

  • 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-05T10:46:44+00:00Added an answer on June 5, 2026 at 10:46 am

    I answered my own question while writing the question (which I find often happens for me). However, I wasn’t able to find much on this anywhere else so I figured I’d share my solution.

    With help from this SO question I came up with this to be able to Mock browser types

            var request = new Mock<HttpRequestBase>(MockBehavior.Strict);
    
            // This line would do the job
            request.Setup(b => b.Browser.Type).Returns("IE9");
    
            var context = new Mock<HttpContextBase>();
            context.SetupGet(c => c.Request).Returns(request.Object);
    
            controller.ControllerContext = new ControllerContext(
                                                context.Object,
                                                new RouteData(),
                                                controller);
    

    I still am uncertain if this is a good idea or not to do in your controller. Any thoughts would be appreciated.

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

Sidebar

Related Questions

Trying to test to see if a user is logged in I am using
Im trying to test my successfully creates a new user after login (using authlogic).
I'm trying to test/spec the following action method public virtual ActionResult ChangePassword(ChangePasswordModel model) {
Trying to test that an event handler gets called on a clicked element with
just trying to test for equality in this piece of code, but getting a
I trying to test an AccountController that uses DotNetOpenAuth but I am running into
I am trying to test out Mybatis's user manual with the result map section.
I'm trying to test an app that creates some custom permissions during intialization. These
I'm trying to unit test a custom action result. I recently watched Jimmy Bogard's
This test is to check that I can return a ViewModel object by creating

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.