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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:01:58+00:00 2026-05-24T01:01:58+00:00

I have the following unit test defined to test my model binder: [TestMethod] public

  • 0

I have the following unit test defined to test my model binder:

[TestMethod]
public void DateTime_Works() {
    // Arrange
    var controllerContext = FakeContext.GetAuthenticatedControllerContext(new Mock<Controller>().Object, "TestAdmin");

    var values = new NameValueCollection {
        { "Foo.Date", "12/02/1964" },
        { "Foo.Hour", "12" },
        { "Foo.Minute", "00" },
        { "Foo.Second", "00" }
    };

    var bindingContext = new ModelBindingContext() { ModelName = "Foo", ValueProvider = new NameValueCollectionValueProvider(values, null) };
    var binder = new DateAndTimeModelBinder();

    // Act
    var result = (DateTime)binder.BindModel(controllerContext, bindingContext);

    // Assert
    Assert.AreEqual(DateTime.Parse("1964-12-02 12:00:00"), result);
}

Here is the FakeContext class:

public static class FakeContext {
    public static HttpContextBase GetHttpContext(string username) {
        var context = new Mock<HttpContextBase>();
        context.SetupGet(ctx => ctx.Request.IsAuthenticated).Returns(!string.IsNullOrEmpty(username));

        return context.Object;
    }

    public static ControllerContext GetControllerContext(Controller controller) {
        return GetAuthenticatedControllerContext(controller, null);
    }

    public static ControllerContext GetAuthenticatedControllerContext(Controller controller, string username) {
        var httpContext = GetHttpContext(username);
        return new ControllerContext(httpContext, new RouteData(), controller);
    }
}

Within my model binder i call a utility method which has the following line in it:

HttpContext.Current.User.Identity.IsAuthenticated

This seems to always return false even when I pass a username in. I was wondering how the GetHttpContext method can be modified to mock this out aswell.

I’d appreciate the help. 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-05-24T01:01:59+00:00Added an answer on May 24, 2026 at 1:01 am

    You are mocking the IsAuthenticated property of the Request and not of the User Identity. Your mock is indicating that the request has been authenticated, but has nothing to do with the identity authentication.

    Changing your mock up a little should fix your problem.

    var identityMock = new Mock<IIdentity>();
    identityMock.SetupGet( i => i.IsAuthenticated ).Returns( !string.IsNullOrEmpty(username));
    
    var userMock = new Mock<IPrincipal>();
    userMock.SetupGet( u => u.Identity ). Returns( identityMock.Object );
    
    var context = new Mock<HttpContextBase>();
    context.SetupGet( ctx => ctx.User ).Returns( userMock.Object );
    

    Edit The more I thought about it the more I felt these mocks needed to be setup seperatly

    Note, I have not executed this code. It should get you what you need however.

    MSDN for Request.IsAuthenticated
    http://msdn.microsoft.com/en-us/library/system.web.httprequest.isauthenticated.aspx

    MSDN for User.Identity.IsAuthenticated
    http://msdn.microsoft.com/en-us/library/system.security.principal.iidentity.isauthenticated.aspx

    Hope this helps.

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

Sidebar

Related Questions

I have defined the following Unit-Test: [TestMethod] //@Test for the Java crowd public void
I have the following unit test: [TestMethod] public void Execute_Sends_Email_To_User() { // Setup InitializeTestEntities();
I have the following unit test: [TestMethod] public void Add_Returns_Edit_View() { // Act ActionResult
I have the following unit test in my application: [TestMethod] public void Windsor_Can_Resolve_HomeController_Dependencies() {
I have the following class structure that I need to unit test: public interface
I have following code: [TestMethod] public void StartWorkInFirstThread() { if (SynchronizationContext.Current == null) SynchronizationContext.SetSynchronizationContext(
I have a unit test which contains the following line of code Site.objects.get(name=UnitTest).delete() and
I have a Vector class, and I was testing the following unit test (using
I have a unit test where I have statically defined a quite large byte
I have the following HttpModule that I wanted to unit test. Problem is I

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.