Has anyone had success in building a Mocking Library to mimic the SLProtocol/SLProtocolExt for unit testing for protocol development?
Hi,
Since DataMiner 10.0, the SLProtocol/SLProtocolExt classes have been changed into interfaces. That makes it possible to mock it for unit testing.
If a Nuget with a mocking framework, like Moq, is being used then it's possible to write unit tests and mock the SLProtocol calls.
For example:
Mock<SLProtocol> protocolMock = new Mock<SLProtocol>();
protocolMock.Setup(p => p.DataMinerID).Returns(dmaId);
protocolMock.Setup(p => p.ElementID).Returns(elementId);
protocolMock.Setup(p => p.ElementName).Returns("Dummy Element Name");
protocolMock.Setup(p => p.GetParameter(testParameterId1)).Returns("DummyValue1");
protocolMock.Setup(p => p.GetParameter(testParameterId2)).Returns("DummyValue2");// Using the mocked SLProtocol
SLProtocol protocol = protocolMock.Object;
string param1 = Convert.ToString(protocol.GetParameter(testParameterId1));
protocol.SetParameter(testSetParameterId, "DummyValue")// Asserting
Assert.AreEqual(param1, "DummyValue1");
protocolMock.Verify(p => p.SetParameter(testSetParameterId, It.Is<string>(incomingValue => VerifyIncomingValue(incomingValue)))); // verify that a setparameter has been called with the expected value// For larger collections, FluentAssertions can be used to have a more easy verification when asserting.
Regards,
Hi,
Mocking the NotifyProtocol calls is indeed a bit more work.
If the unit test can run against a more recent DataMiner version (10.1.1 (RN 27995) onwards, then some notifies can be mocked with the alternative method. For example NotifyProtocol(149 /*NT_ADD_ROW*/) has an alternative method protocol.AddRow(). NotifyProtocol(220 /*NT_FILL_ARRAY_WITH_COLUMN*/) has as alternative protocol.FillArrayWithColumn(). These methods used to be extension methods that could not be mocked, but since 10.1.1 this has become part of the SLProtocol interface.
Of course some of these methods and the NotifyProtocol method still could rely on object arrays as input or return value and then one still needs to create these objects.
For example to simulate the response of a NotifyProtocol 321, it could then look like this:
object[] tableInputCols = new object[4];
tableInputCols[0] = pkCol;
tableInputCols[1] = inputLabelCol;
tableInputCols[2] = stateCol;
tableInputCols[3] = lockedCol;
protocolMock.Setup(p => p.NotifyProtocol(321, inputsTableParameterId, It.Is(ui => ui.Length == 4 && ui[0] == 0 && ui[1] == 1 && ui[2] == 2 && ui[3] == 3))).Returns(tableInputCols);
If the assertion of the executed calls is a bit larger, then custom created methods could be created to make it more readable.
protocolMock.Verify(p => p.NotifyProtocol(220, It.Is(tablePids => VerifyTablePidInfo(tablePids, expectedTablePids)), It.Is(tableContent => VerifyTableInfo(tableContent, expectedTableContent))));
The methods VerifyTablePidInfo() and VerifyTableInfo() then validate the content of the objects that were used to execute that notify 220 and return a boolean if it was as expected or not.
An alternative for the verification of the content of received object arrays is to use the FluentAssertions NuGet package in the unit test. Then it’s possible to execute something like expectedDataObjectArray.Should().BeEquivalentTo(receivedDataObjectArray); , if the content is not equal then the unit test will fail.
As far as I’m aware there are no wrappers or libraries yet available to help mock or validate the table manipulation calls.
Thanks Laurens, we’ve been able to mock both setting and getting of standalone parameters and tables however, for tables it has proven to be quite an involved process as we’ve been required to mock the NotifyProtocol method which depends on an object array representation of tables that is hard to mimic in code. Are there any wrappers or libraries built from Skyline which can aid in mocking table manipulation behaviour in a more object-oriented manner?
Alternatively, should we be using other methods to read and write into tables?