Home > Net >  New virtual creates a new method instead of hiding
New virtual creates a new method instead of hiding

Time:07-25

The requirement here is to write test cases for the Opc Ua NodeManager and it uses NodeId from Opc.Ua class.

Methods/properties in NodeId class cannot be moq because they are Non-Overridable methods and have ony get in them.

So I created a wrapper on top of NodeId class and tried to Moq that class. It works fine but now I have 2 methods/Properties

public class NodeIdTestClass : NodeId
{
    public NodeIdTestClass()
    {

    }
    public new virtual object Identifier
    {
        get => base.Identifier;
    }

    public new virtual ushort NamespaceIndex
    {
        get => base.NamespaceIndex;
    }

    public new virtual bool IsNullNodeId
    {
        get => base.IsNullNodeId;
    }
}

//Arrange

        var nodeIdMock = new Mock<NodeIdTestClass>() { CallBase = true };
        nodeIdMock.Setup(x => x.Identifier).Returns(nodeIdMock.Object.Identifier);
        nodeIdMock.Setup(x => x.NamespaceIndex).Returns(1);
        nodeIdMock.Setup(x => x.IsNullNodeId).Returns(false);
        _nodemanager.SetNamespaces(new string[] { "0", "1", "2" });

        //Act
        var result = _nodemanager.GetManagerHandle(nodeIdMock.Object);

The problem :

Problem Object Highlighted

Is there something wrong with code?

CodePudding user response:

Do you need a mock?

Wouldn't this be enough?

var nodeId = new NodeId(value: 17, namespaceIndex: 1);
    
var result = _nodemanager.GetManagerHandle(nodeId);
  • Related