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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:32:57+00:00 2026-05-16T23:32:57+00:00

I am trying to unit test an action filter I wrote. I want to

  • 0

I am trying to unit test an action filter I wrote. I want to mock the HttpClientCertificate but when I use MOQ I get exception. HttpClientCertificate doesnt have a public default constructor.

code:

//Stub HttpClientCertificate </br>
var certMock = new Mock<HttpClientCertificate>();
HttpClientCertificate clientCertificate = certMock.Object;
requestMock.Setup(b => b.ClientCertificate).Returns(clientCertificate);
certMock.Setup(b => b.Certificate).Returns(new Byte[] { });
  • 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-16T23:32:58+00:00Added an answer on May 16, 2026 at 11:32 pm

    This is the most awkward case of creating unit testable systems in .NET. I invariable end up adding a layer of abstraction over the component that I can’t mock. Normally this is required for classes with inaccessible constructors (like this case), non-virtual methods or extension methods.

    Here is the pattern I use (which I think is Adapter pattern) and is similar to what MVC team has done with all the RequestBase/ResponseBase classes to make them unit testable.

    //Here is the original HttpClientCertificate class
    //Not actual class, rather generated from metadata in Visual Studio
    
    public class HttpClientCertificate : NameValueCollection {
        public byte[] BinaryIssuer { get; }
        public int CertEncoding { get; }
        //other methods
        //...
    }
    
    public class HttpClientCertificateBase {
        private HttpClientCertificate m_cert;
    
        public HttpClientCertificateBase(HttpClientCertificate cert) {
           m_cert = cert;
        }
        public virtual byte[] BinaryIssuer { get{return m_cert.BinaryIssuer;} }
        public virtual int CertEncoding { get{return m_cert.CertEncoding;} }
        //other methods
        //...
    }
    
    public class TestClass {
      [TestMethod]
      public void Test() {
          //we can pass null as constructor argument, since the mocked class will never use it and mock methods will be called instead
          var certMock = new Mock<HttpClientCertificate>(null);
          certMock.Setup(cert=>cert.BinaryIssuer).Returns(new byte[1]);
      }
    }
    

    In your code that uses HttpClientCertificate you instead use HttpClientCertificateBase, which you can instantiate like this – new HttpClientCertificateBase(httpClientCertificateInstance). This way you are creating a test surface for you to plug in mock objects.

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

Sidebar

Related Questions

I am trying to unit test a controller action that uses UpdateModel but I
I'm trying to unit test my websocket on node.js and want to mock out
I'm trying to write a unit test that will loop through all action methods
I'm trying to unit test a custom action result. I recently watched Jimmy Bogard's
I'm trying to unit test, using VS unit testing facility following method. void Get(string
I'm trying to mock up DomainContext calls for my unit test. My real code
I'm trying to unit test a custom ConfigurationElementCollection , but I'm having a problem
I am running on Castle's trunk, and trying to unit-test a controller-action where validation
I am trying to do a unit test on the list action of a
Im trying to unit test my controllers, but every tutorial I found on the

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.