Home > Software design >  C# test cases for ICloneable
C# test cases for ICloneable

Time:12-06

how to get coverage for object ICloneable.Clone() method while writing test cases.

 #region ICloneable Members

    object ICloneable.Clone()
    {
        return this.Clone();
    }

    public new Blue Clone()
    {
        Blue _temp = (Blue)this.MemberwiseClone();
        _temp.Node = Node.Clone();

        return _temp;
    }

    #endregion

The current coverage looks like

the coverage look like.

CodePudding user response:

While these can be separate cases, here is a really simplified example of testing/covering the shown code.

//Arrange
Blue expected = new(); //populate as needed

//Act
Blue a = expected.Clone();
Blue b = (Blue)((ICloneable)expected).Clone();

//Assert - using FluentAsertions - cases should be self explanatory 
a.Should().BeEquivalentTo(b); 
a.Should().BeEquivalentTo(expected);
b.Should().BeEquivalentTo(expected);
  • Related