I am trying to create a unit test framework using VS built in unit testing and using the Moq framework.
Quite a few of the methods under test are static and are within a static class. For example:
public static class clsLog
{
private static SysLog dbDatabase;
private static string sComputerName;
}
My unit test code is this
using Moq;
using System;
using System.IO.Ports;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ClassLib_Utilities;
using FragmentAnalyzer;
public class clsAutomationInterfaceTests
{
private Mock<SerialPort> mockSerialPort;
private Mock<Thread> mockThread;
private Mock<clsLog> mockLog = new Mock<clsLog>;
[TestInitialize]
public void Setup()
{
// Setup mocks and initializations
mockSerialPort = new Mock<SerialPort>();
mockThread = new Mock<Thread>();
mockLog = new Mock<clsLog>();
// Override default constructor for the test
// Assuming clsAutomationInterface has dependency injection for easier testing
}
}
The following line above:
private Mock mockLog = new Mock;
results in a compiler error: ‘clsLog’: static types cannot be used as type arguments
I would rather not go through all of the classes and change them to public, unless that is the only resort.
Is there another way to use static classes as Mock parameters in unit tests?
I did change the static class to non static and it does work that way.
JETMike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.