Simsons
Simsons

Reputation: 12745

Creating Mock reference and setup methods for complex operations using MOQ

I have two methods , OpenCertificateStore and FindCertificateBySubjectName and implemented them as following:

 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 my unit test as 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 fine , but it would be lot better if I can find a way to mock myCertHelper.

If I do mok them , it returns null as it's not querying actual certificate store.

Upvotes: 3

Views: 710

Answers (1)

RubberDuck
RubberDuck

Reputation: 12748

How do you mock MyCertificateHelper?

You don't.

Doing so would have no benefit. If you did, then all of the classes in your test would be mocked out and you would no longer actually be testing any of your code. At that point, you might as well delete the test. It wouldn't do anything but cost you money to maintain it.


  • Prefixing everything with My is useless. Worse than useless, it's noisy and distracting. Drop it.
  • I don't like the temporal coupling in your design. I don't like needing to call methods like Open or Init. It's easy to forget to call it or call it too many times. It's better if the constructor puts the class into a usable state.
  • It's nice that you're injecting and kicking the logger, but I find injecting loggers to be a code smell. I find it's much nicer to have my classes raise events and have the logger listen for those events. It removes the need to mock the logger all the time and provides nice hooks for other code to leverage. This kind of event driven design ends up in code that is much more open to extension, but closed for modification.

Upvotes: 3

Related Questions