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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:16:25+00:00 2026-05-13T22:16:25+00:00

I trying to test an AccountController that uses DotNetOpenAuth but I am running into

  • 0

I trying to test an AccountController that uses DotNetOpenAuth but I am running into a problem. I want to test the Logon Actionresult to see that it is returning the correct views. The test fails because realm(I think) has a contract that requires the HttpContext.Current not to be null. I think I have to mock the request somehow but I am not sure how I should do this.

This is the ActionResult code. It’s taken directly from a DotNetOpenAuth example.

[AcceptVerbs(HttpVerbs.Post), ValidateAntiForgeryToken]
public ActionResult LogOn(string openid_identifier, 
                          bool rememberMe, 
                          string returnUrl)
{
    Identifier userSuppliedIdentifier;
    if (Identifier.TryParse(openid_identifier, out userSuppliedIdentifier))
    {
        try
        {
            var request = this.RelyingParty
                              .CreateRequest(openid_identifier,
                                             Realm.AutoDetect, 
                                             Url.ActionFull("LogOnReturnTo"));

            if (!string.IsNullOrEmpty(returnUrl))
            {
                request.SetUntrustedCallbackArgument("returnUrl", returnUrl);
            }
            return request.RedirectingResponse.AsActionResult();
        }
        catch (ProtocolException ex)
        {
            ModelState.AddModelError("OpenID", ex.Message);
        }
    }
    else
    {
        ModelState.AddModelError("openid_identifier", 
                                 "This doesn't look like a valid OpenID.");
    }
    return RedirectToAction("LogOn", "Account");
}

Thanks in advance,

Pickels

  • 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-13T22:16:25+00:00Added an answer on May 13, 2026 at 10:16 pm

    If one of the Controller’s dependencies requires that HttpContext.Current is available, you can’t really mock it out directly, but you can wrap that dependency in a testable abstraction itself.

    If we assume that Realm is the culprit, you must first extract an interface from it:

    public interface IRealm
    {
        // I don't know what the real AutoDetect property returns,
        // so I just assume bool
        bool AutoDetect { get; }
    }
    

    You would obviously need a real implementation of IRealm:

    public class RealmAdapter : IRealm
    {
        bool AutoDetect { get { return Realm.AutoDetect; } }
    }
    

    You must inject the abstract IRealm into the controller, for instance by using Constructor Injection

    public class MyController
    {
        private readonly IRealm realm;
    
        public MyController(IRealm realm)
        {
            if( realm == null)
            {
                throw new ArgumentNullException("realm");
            }
    
            this.realm = realm;
        }
    }
    

    You can now change your implementation of the LogOn method to use this.realm instead of relying directly on the Realm class.

    A unit test would now be able to supply a mock IRealm instance to the controller:

    var realmMock = new Mock<IRealm>();
    var sut = new MyController(realmMock.Object);
    

    (This example uses Moq.)

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

Sidebar

Related Questions

just trying to test for equality in this piece of code, but getting a
I'm trying to test some mailers with rspec but deliveries are always empty. Here
I am trying to test a sinatra app using minitest and capybara but get
I'm trying to test/spec the following action method public virtual ActionResult ChangePassword(ChangePasswordModel model) {
When trying to test the speed of functions, I found that not all parts
I'm trying to test my Session Beans with JUnit, but I can't. I've tried
I'm trying to test a model that has an implicit has_many association, and am
Trying to test that an event handler gets called on a clicked element with
I'm trying to test some TPL dataflow code and run into what looks like
I'm trying to test an app that creates some custom permissions during intialization. These

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.