Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>Here is the pattern I use (which I think is <a href="http://en.wikipedia.org/wiki/Adapter_pattern" rel="nofollow">Adapter pattern</a>) and is similar to what MVC team has done with all the <code>RequestBase</code>/<code>ResponseBase</code> classes to make them unit testable.</p> <pre><code>//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&lt;HttpClientCertificate&gt;(null); certMock.Setup(cert=&gt;cert.BinaryIssuer).Returns(new byte[1]); } } </code></pre> <p>In your code that uses <code>HttpClientCertificate</code> you instead use <code>HttpClientCertificateBase</code>, which you can instantiate like this - <code>new HttpClientCertificateBase(httpClientCertificateInstance)</code>. This way you are creating a test surface for you to plug in mock objects.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload