How to create a minimal dummy X509Certificate2?

前端 未结 4 1942
暖寄归人
暖寄归人 2021-01-01 11:29

I\'m unit testing a .NET application; some of the unit tests involve programmatically generating X509Certificate2 objects.

I don\'t care about actual signing/private

4条回答
  •  爱一瞬间的悲伤
    2021-01-01 12:34

    At Langdon's request, the solution I used, myself:

            //Moling test Certificate
            var cert = new MX509Certificate2();
            var subject = new MX500DistinguishedName();
            // hookup
            cert.SubjectNameGet = () => subject;
            cert.ThumbprintGet = () => "foo";
            subject.NameGet = () => "foobar";
    

    This worked because the only fields I accessed in the Certificate were it's SubjectName and Thumbprint, and the only field of the SubjectName I accessed was the name. I "moled" the getter methods for these fields to return the dummy strings. If you were to access other fields, you'd probably need to mole them to.

    And so, what is "Moles"?

    "Moles is a lightweight framework for test stubs and detours in .NET that is based on delegates. Moles may be used to detour any .NET method, including non-virtual/static methods in sealed types. Moles is freely available on Visual Studio Gallery or bundled with Pex."

    http://research.microsoft.com/en-us/projects/moles/

提交回复
热议问题