How to mock a OPC UA Session for unit testing in C#

1.4k Views Asked by At

I have a C# application, which includes a service that works with multiple OPC UA Sessions (UnifiedAutomation.UaClient.Session). These sessions are created by connecting to addresses like opc.tcp://localhost:48030 etc.

foreach (ConnectionStringSettings connectionSettings in ConfigurationManager.ConnectionStrings)
{
    var connectionString = connectionSettings.ConnectionString;

    // Ignore non-OPC-connections 
    if (!connectionString.StartsWith("opc.tcp")) continue;

    // Create new session for connection
    var session = new Session
    {
        AutomaticReconnect = true,
        ReconnectDelay = 0
    };

    // Connect to OPC UA Server
    try
    {
        session.Connect(connectionString, SecuritySelection.None);
        Log("OPC UA Session establish.", EventLogEntryType.Information);
    }
    catch (StatusException)
    {
        Log($"No OPC UA Server found at {connectionString}.",
            EventLogEntryType.Warning);
    }
}

I want to unit test the methods of my service but I cannot figure out how to mock the OPC UA sessions.

Any idea if this is possible at all?

0

There are 0 best solutions below