There is class Driver, which is calling static extern method in constructor. Which is the best way to change architecture to be able to unit test Driver method.
public class Driver
{
[DllImport("Driver.dll")]
private static extern IntPtr CreateDriver();
// Pointer to C++ Driver object
internal IntPtr DriverPtr; // this for integration tests
public Driver()
{
DriverPtr = CreateDriver();
}
public int SomeMethod(int a) // test only this method
{
return a + 1;
}
}
Your story isn't entirely clear, but I think you're asking how to test the code without having to interface with the actual driver DLL. It is unclear whether you want to use the value assigned to DriverPtr
in your constructor.
You can fix this by moving your static methods into an instance class, interfacing that class and mocking it during tests.
The interface:
public interface IDriverProvider
{
IntPtr Create();
}
Implementation:
public class DriverProvider : IDriverProvider
{
[DllImport("Driver.dll")]
private static extern IntPtr CreateDriver();
public IntPtr Create()
{
return CreateDriver();
}
}
Then inject it into your using class:
public class Driver
{
internal IntPtr DriverPtr;
public Driver(IDriverProvider driverProvider)
{
DriverPtr = driverProvider.Create();
}
public int SomeMethod(int a) // test this method
{
return a + 1;
}
}
Now you can test your class:
// Arrange
var driverProviderMock = new Mock<IDriverProvider>();
var driver = new Driver(driverProviderMock.Object);
// Act
driver.SomeMethod();
// Assert
driverProviderMock.Verify(d => d.Create());