Create Mock Links and Customization Methods for Complex Operations Using MOQ

I have two methods, OpenCertificateStore and FindCertificateBySubjectName, and they are implemented like this:

 public void OpenCertificateStore()
        {
            if (_certificateStore == default(X509Store))
                _certificateStore = new X509Store(StoreLocation.CurrentUser);

            _certificateStore.Open(OpenFlags.ReadOnly | OpenFlags.IncludeArchived);
        }

        public X509Certificate2Collection FindCertificateBySubjectName(string certificateSubjectName)
        {
            X509Certificate2Collection certificates = new X509Certificate2Collection();
            if (_certificateStore != default(X509Store))
            {
                certificates = _certificateStore.Certificates.Find(X509FindType.FindBySubjectName, certificateSubjectName, true);
            }

            return certificates;
        }

      

I have a unit test like below:

[TestClass]
    public class MyHealthTests
    {
        private Mock<Logger> _logger;
        private Mock<MYCertificateManager> _certManager;

        [TestInitialize]
        public void Initialize()
        {
             _logger = new Mock<Logger>();
             _certManager = new Mock<MYCertificateManager>();
        }

        [TestMethod]
        public void PassName_FindCertiFicatebyName_ShouldReturnValid()
        {


            MyCertificateHelper myCertHelper = new MyCertificateHelper(_logger.Object,_certManager.Object);

            myCertHelper.OpenCertificateStore();
            var certNameCollection = myCertHelper.FindCertificateBySubjectName("Valid Cert Name");
            Assert.IsNotNull(certNameCollection);
            Assert.IsTrue(certNameCollection.Count > 0);
        }
    }

      

Which works great, but it would be much better if I could find a way to mock myCertHelper

.

If I mok them it returns null as it does not ask for the actual certificate store.

+3


source to share


1 answer


How are you kidding MyCertificateHelper

me?

Not.

It won't do any good. If so, then all classes in your test will be mocked and you will no longer test any of your codes. At this point, you can delete the test. It would have done nothing but spend money to maintain it.




  • The prefix for everything with is My

    useless. Worse than useless, it is noisy and distracting. Give it up.
  • I don't like the temporary link in your design. I don't like calling methods like Open

    or Init

    . It's easy to forget to name it, or to call it too many times. It's better if the constructor puts the class in a usable state.
  • It's good that you enter and kick the logger, but I find the injecting logs are code smells. I find it much nicer when my classes raise events and write logs to those events. This eliminates the need to constantly mock the log and provides good bindings for using other code. This event-driven design ends up with code that is much more open to extensibility but closed to modification.
+2


source







All Articles