Reputation: 12745
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
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.
My
is useless. Worse than useless, it's noisy and distracting. Drop it. 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. Upvotes: 3