I am developing an .NET core OPC client on VS2019 using a commercial library to communicate with an OPC server. I am trying to test my code with Nunit and FakeItEasy. I need to Fake calls to a class Discovery but the members are non-virtual and FakeItEasy is unable to fake them. This is code that calls a Discovery method.
public List<ApplicationDescription> FindServers(ApplicationInstanceBase applicationInstance, string serverName, string serverPort)
{
try
{
// Discovery URL
_discoveryUrl = "opc.tcp://" + serverName + ":" + serverPort;
Discovery discovery = new Discovery(applicationInstance);
return discovery.FindServers(_discoveryUrl);
}
catch (Exception)
{
}
}
And this is the test code
[Test]
public void FindServersTest__simple()
{
_serverList = CreateServerList(); //create 2 dummy servers
Discovery _discovery = A.Fake<Discovery>(x => x.WithArgumentsForConstructor(() => new Discovery(A<ApplicationInstanceBase>.Ignored)));
A.CallTo(() => _discovery.FindServers(A<string>.Ignored)).Returns(_serverList);
List<ApplicationDescription> servers = _opcClient.FindServers(_applicationInstance, _OPCServer, _OPCServerPort);
Assert.AreEqual(servers.Count, 2);
}
I get the following error message
Message: FakeItEasy.Configuration.FakeConfigurationException :
The current proxy generator can not intercept the method UnifiedAutomation.UaClient.Discovery.FindServers(System.String discoveryUrl) for the following reason: - Non-virtual members can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted.
Because this is a commercial library I cannot make any changes to their code. Any ideas how I can get round this?