SOLIDWORKS C# API - Test SelectSketchPlane Method

12 minute read

Objective

I want to:

  • Test SelectSketchPlane Method.
  • I will not explain each line, since they are already explained in previous articles.
  • We will continue from previous article ๐Ÿš€ Test ApplyUnitConversion Method.

Demo Video

Watch the video below to learn how to Test SelectSketchPlane Method.


Please note that there are no explanation in the video.
Explanation of each step and why we write code this way is given in this post.


Modify [MainWindowViewModel]

First, we need to make method we want to test and members we going to use in our test class make as public.

  • Public Method:
    • public virtual bool SelectSketchPlane(SldWorks.SldWorks swApp, ModelDoc2 swDoc)

This is the change we need to do in our MainWindowViewModel class.


Add Test Cases

Now we set up Test cases for below ๐Ÿ‘‡๐Ÿป SelectSketchPlane method of _viewModel variable.

public bool SelectSketchPlane(SldWorks.SldWorks swApp, ModelDoc2 swDoc)
{
  bool boolStatus = swDoc.Extension.SelectByID2("Right Plane", "PLANE", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault);

  if (boolStatus == false)
  {
    messageToShow = "Failed to select Right Plane";
    swApp.CloseAllDocuments(true);
    swApp.ExitApp();
    return false;
  }

  swDoc.SketchManager.InsertSketch(false);

  double x1, y1, z1, x2, y2, z2;
  var lengthUnit = swDoc.LengthUnit;

  conversionHelper.UnitConversion((swLengthUnit_e)lengthUnit);

  (x1, y1, z1) = ApplyUnitConversion(StartPointViewModel, conversionHelper.LengthConversionFactor);
  (x2, y2, z2) = ApplyUnitConversion(EndPointViewModel, conversionHelper.LengthConversionFactor);
  SketchSegment sketchSegment = null;
  return CreateLine(sketchSegment, x1, y1, z1, swApp, swDoc.SketchManager, x2, y2, z2);
}

To test SelectSketchPlane method, we need 2 test cases.

  1. TestCase 1 : When Plane selection Succeeds Returns True
  2. TestCase 2 : When Plane selection Fails Returns False

Before we add test cases, we will create a region called โ€œTest Method [SelectSketchPlane]โ€.

This will help organize our code properly.


Add [TestCase 1]

In this section, we set up TestCase 1.

Please see below ๐Ÿ‘‡๐Ÿป code sample for set up.

[Fact]
public void SelectSketchPlane_SuccessfullySelectsPlane_ReturnsTrue()
{
  // Arrange
  _mockSwDoc.Setup(d => d.Extension).Returns(_mockModelDocExtension.Object);
  _mockSwDoc.Setup(d => d.SketchManager).Returns(_mockSketchManager.Object);
  _mockModelDocExtension.Setup(e => e.SelectByID2("Right Plane", "PLANE", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault)).Returns(true);
  _mockSwDoc.Setup(d => d.LengthUnit).Returns((int)swLengthUnit_e.swINCHES);
  _mockConversionHelper.Setup(c => c.UnitConversion(It.IsAny<swLengthUnit_e>()));
  _mockConversionHelper.Setup(c => c.LengthConversionFactor).Returns(0.0254);

  _mockEndPoint.Setup(p => p.XPoint).Returns(2);
  _mockEndPoint.Setup(p => p.YPoint).Returns(3);
  _mockEndPoint.Setup(p => p.ZPoint).Returns(0);

  _mockSketchManager.Setup(sm => sm.CreateLine(It.IsAny<double>(), It.IsAny<double>(), It.IsAny<double>(), It.IsAny<double>(), It.IsAny<double>(), It.IsAny<double>()))
                    .Returns(_mockSketchSegment.Object);

  // Act
  var result = _viewModel.SelectSketchPlane(_mockSwApp.Object, _mockSwDoc.Object);

  // Assert
  Assert.True(result);
  _mockSketchManager.Verify(s => s.InsertSketch(false), Times.Once);
  _mockConversionHelper.Verify(c => c.UnitConversion(It.IsAny<swLengthUnit_e>()), Times.Once);
}

Explanation of above SelectSketchPlane_SuccessfullySelectsPlane_ReturnsTrue is give below.

[Fact]
public void SelectSketchPlane_SuccessfullySelectsPlane_ReturnsTrue()
{

}
  • [Fact] :
    • This is a test attribute.
    • This attribute shows that this method is a test case that the xUnit test runner recognizes.
  • public :
    • This means that the SelectSketchPlane_SuccessfullySelectsPlane_ReturnsTrue method can be accessed from anywhere.
    • This is important, because xUnit test runner is external agent.
    • xUnit test runner need to have accessed to this SelectSketchPlane_SuccessfullySelectsPlane_ReturnsTrue method.
    • Because of this requirment we need to give public accessor.
  • void :
    • This is the return type of SelectSketchPlane_SuccessfullySelectsPlane_ReturnsTrue method.
    • Generally we donโ€™t return anything from test method.
    • Because of this we return void means we are not returning anything.
  • SelectSketchPlane_SuccessfullySelectsPlane_ReturnsTrue
    • This is the name of method.
    • This name is created in 3 different parts and combined with underscore โ€œ_โ€œ.
    • Different parts are explained below ๐Ÿ‘‡๐Ÿป:
      • Part 1: Name of method we are testing.
      • Part 2: Return value we are expecting.
      • Part 3: Condition for which we are testing the method.
_mockSwDoc.Setup(d => d.Extension).Returns(_mockModelDocExtension.Object);
  • _mockSwDoc:
    • This is mocking object for ModelDoc2 object.
  • _mockSwDoc.Setup():
    • We are using the Setup() method from _mockSwDoc object.
    • This is important and most basic one you should learn.
    • This Setup() method allow us to setup the behavior of object it is mocking and its members/methods.
    • Please follow me and you will be able to understand how we setup the child members/methods.
  • Setup(d => d.Extension)
    • d is object we are mocking.
    • d represent ModelDoc2.
    • From this d object we will setup Extension property.
  • Returns(_mockModelDocExtension.Object)
    • Here are setting up return value of Extension property.
    • Above we are saying, whenever Extension property called, return ModelDocExtension object from _mockModelDocExtension.Object variable.
_mockSwDoc.Setup(d => d.SketchManager).Returns(_mockSketchManager.Object);
  • _mockSwDoc:
    • This is mocking object for ModelDoc2 object.
  • _mockSwDoc.Setup():
    • We are using the Setup() method from _mockSwDoc object.
    • This is important and most basic one you should learn.
    • This Setup() method allow us to setup the behavior of object it is mocking and its members/methods.
    • Please follow me and you will be able to understand how we setup the child members/methods.
  • Setup(d => d.SketchManager)
    • d is object we are mocking.
    • d represent ModelDoc2.
    • From this d object we will setup SketchManager property.
  • Returns(_mockSketchManager.Object)
    • Here are setting up return value of SketchManager property.
    • Above we are saying, whenever SketchManager property called, return SketchManager object from _mockSketchManager.Object variable.
_mockModelDocExtension.Setup(e => e.SelectByID2("Right Plane", "PLANE", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault)).Returns(true);
  • _mockModelDocExtension:
    • This is mocking object for ModelDocExtension object.
  • _mockModelDocExtension.Setup():
    • We are using the Setup() method from _mockModelDocExtension object.
    • This is important and most basic one you should learn.
    • This Setup() method allow us to setup the behavior of object it is mocking and its members/methods.
    • Please follow me and you will be able to understand how we setup the child members/methods.
  • Setup(e => e.SelectByID2("Right Plane", "PLANE", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault))
    • e is object we are mocking.
    • e represent ModelDocExtension.
    • From this e object we will setup SelectByID2 method.
  • Returns(_mockSketchManager.Object)
    • Here are setting up return value of SelectByID2 method.
    • Above we are saying, whenever SelectByID2 method called, return true from _mockSketchManager.Object variable.
_mockSwDoc.Setup(d => d.LengthUnit).Returns((int)swLengthUnit_e.swINCHES);
  • _mockSwDoc:
    • This is mocking object for ModelDoc2 object.
  • _mockSwDoc.Setup():
    • We are using the Setup() method from _mockSwDoc object.
    • This is important and most basic one you should learn.
    • This Setup() method allow us to setup the behavior of object it is mocking and its members/methods.
    • Please follow me and you will be able to understand how we setup the child members/methods.
  • Setup(d => d.LengthUnit)
    • d is object we are mocking.
    • d represent ModelDoc2.
    • From this d object we will setup LengthUnit property.
  • Returns((int)swLengthUnit_e.swINCHES)
    • Here are setting up return value of LengthUnit property.
    • Above we are saying, whenever LengthUnit property called, return (int)swLengthUnit_e.swINCHES) from _mockSketchManager.Object variable.
_mockConversionHelper.Setup(c => c.UnitConversion(It.IsAny<swLengthUnit_e>()));
  • _mockConversionHelper:
    • This is mocking object for IUnitConversionHelper object.
  • _mockConversionHelper.Setup():
    • We are using the Setup() method from _mockConversionHelper object.
    • This is important and most basic one you should learn.
    • This Setup() method allow us to setup the behavior of object it is mocking and its members/methods.
    • Please follow me and you will be able to understand how we setup the child members/methods.
  • Setup(c => c.UnitConversion(It.IsAny<swLengthUnit_e>()))
    • c is object we are mocking.
    • c represent IUnitConversionHelper.
    • From this c object we will setup UnitConversion method.
    • This UnitConversion method will take any value of It.IsAny<swLengthUnit_e>().
    • This time we did not Return any value, we just want to setup UnitConversion method.
_mockConversionHelper.Setup(c => c.LengthConversionFactor).Returns(0.0254);
  • _mockConversionHelper:
    • This is mocking object for ModelDoc2 object.
  • _mockConversionHelper.Setup():
    • We are using the Setup() method from _mockSwDoc object.
    • This is important and most basic one you should learn.
    • This Setup() method allow us to setup the behavior of object it is mocking and its members/methods.
    • Please follow me and you will be able to understand how we setup the child members/methods.
  • Setup(c => c.LengthConversionFactor)
    • c is object we are mocking.
    • c represent IUnitConversionHelper.
    • From this c object we will setup LengthConversionFactor property.
  • Returns(0.0254)
    • Here are setting up return value of LengthConversionFactor property.
    • Above we are saying, whenever LengthConversionFactor property called, return 0.0254.
_mockEndPoint.Setup(p => p.XPoint).Returns(2);
  • _mockEndPoint:
    • This is mocking object for IPointViewModel object.
  • _mockEndPoint.Setup():
    • We are using the Setup() method from _mockEndPoint object.
    • This is important and most basic one you should learn.
    • This Setup() method allow us to setup the behavior of object it is mocking and its members/methods.
    • Please follow me and you will be able to understand how we setup the child members/methods.
  • Setup(p => p.XPoint)
    • p is object we are mocking.
    • p represent IPointViewModel.
    • From this p object we will setup XPoint property.
  • Returns(2)
    • Here are setting up return value of XPoint property.
    • Above we are saying, whenever XPoint property called, return 2.
_mockEndPoint.Setup(p => p.YPoint).Returns(3);
  • _mockEndPoint:
    • This is mocking object for IPointViewModel object.
  • _mockEndPoint.Setup():
    • We are using the Setup() method from _mockEndPoint object.
    • This is important and most basic one you should learn.
    • This Setup() method allow us to setup the behavior of object it is mocking and its members/methods.
    • Please follow me and you will be able to understand how we setup the child members/methods.
  • Setup(p => p.YPoint)
    • p is object we are mocking.
    • p represent IPointViewModel.
    • From this p object we will setup YPoint property.
  • Returns(3)
    • Here are setting up return value of YPoint property.
    • Above we are saying, whenever YPoint property called, return 3.
_mockEndPoint.Setup(p => p.ZPoint).Returns(3);
  • _mockEndPoint:
    • This is mocking object for IPointViewModel object.
  • _mockEndPoint.Setup():
    • We are using the Setup() method from _mockEndPoint object.
    • This is important and most basic one you should learn.
    • This Setup() method allow us to setup the behavior of object it is mocking and its members/methods.
    • Please follow me and you will be able to understand how we setup the child members/methods.
  • Setup(p => p.ZPoint)
    • p is object we are mocking.
    • p represent IPointViewModel.
    • From this p object we will setup ZPoint property.
  • Returns(0)
    • Here are setting up return value of ZPoint property.
    • Above we are saying, whenever ZPoint property called, return 0.
// Arrange
_mockSketchManager.Setup(sm => sm.CreateLine(It.IsAny<double>(), It.IsAny<double>(), It.IsAny<double>(), It.IsAny<double>(), It.IsAny<double>(), It.IsAny<double>()))
                  .Returns(_mockSketchSegment.Object);
  • _mockSketchManager:
    • This is mocking object for SketchManager object.
  • _mockSketchManager.Setup():
    • We are using the Setup() method from _mockSketchManager object.
    • This is important and most basic one you should learn.
    • This Setup() method allow us to setup the behavior of object it is mocking and its members/methods.
      • Please follow me and you will be able to understand how we setup the child members/methods.
  • Setup(sm => sm.CreateLine(It.IsAny<double>(), It.IsAny<double>(), It.IsAny<double>(), It.IsAny<double>(), It.IsAny<double>(), It.IsAny<double>()))
    • sm is object we are mocking.
    • sm represent SketchManager.
    • From this sm object we will setup CreateLine method.
    • This method takes 6 double parameters.
    • We are defining that all 6 parameters will take any double value.
  • Returns(_mockSketchSegment.Object)
    • Here are setting up return value of CreateLine method.
    • Above we are saying, whenever CreateLine method called, return SketchSegment object from _mockSketchSegment.Object variable.
// Act
bool result = _viewModel.SelectSketchPlane(_mockSwApp.Object, _mockSwDoc.Object);

In above code, we are calling SelectSketchPlane method of _viewModel variable and store the return value into result variable.

  • _viewModel:
    • ViewModel variable whose method we want to test.
  • SelectSketchPlane:
    • Method we want to test.
  • SelectSketchPlane(_mockSwApp.Object, _mockSwDoc.Object)
    • Calling SelectSketchPlane method and passing parameters to method.
    • _mockSwApp.Object: SldWorks object from _mockSwApp variable.
    • _mockSwDoc.Object: ModelDoc2 object from _mockSwDoc variable.
// Assert
Assert.True(result);
_mockSketchManager.Verify(s => s.InsertSketch(false), Times.Once);
_mockConversionHelper.Verify(c => c.UnitConversion(It.IsAny<swLengthUnit_e>()), Times.Once);
  • Assert.True(result);:
    • We are checking value of result variable is true.
    • If return value is true then our asseertion is true.
  • _mockSketchManager.Verify(s => s.InsertSketch(false), Times.Once);:
    • Here we are verifying InsertSketch() method is called 1 time only.
    • We need to do this to make sure our code did not run too much.
  • _mockConversionHelper.Verify(c => c.UnitConversion(It.IsAny<swLengthUnit_e>()), Times.Once);:
    • Here we are verifying UnitConversion() method is called 1 time only.
    • We need to do this to make sure our code did not run too much.

Add [TestCase 2]

In this section, we set up TestCase 2.

Please see below ๐Ÿ‘‡๐Ÿป code sample for set up.

[Fact]
public void SelectSketchPlane_FailsToSelectPlane_ReturnsFalse()
{
    // Arrange
    _mockSwDoc.Setup(d => d.Extension).Returns(_mockModelDocExtension.Object);
    _mockModelDocExtension.Setup(e => e.SelectByID2("Right Plane", "PLANE", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault)).Returns(false);

    // Act
    var result = _viewModel.SelectSketchPlane(_mockSwApp.Object, _mockSwDoc.Object);

    // Assert
    Assert.False(result);
    Assert.Equal("Failed to select Front Plane", _viewModel.messageToShow);
    _mockSwApp.Verify(a => a.CloseAllDocuments(true), Times.Once);
    _mockSwApp.Verify(a => a.ExitApp(), Times.Once);
}

Explanation of above SelectSketchPlane_FailsToSelectPlane_ReturnsFalse is give below.

[Fact]
public void SelectSketchPlane_FailsToSelectPlane_ReturnsFalse()
{

}
  • [Fact] :
    • This is a test attribute.
    • This attribute shows that this method is a test case that the xUnit test runner recognizes.
  • public :
    • This means that the SelectSketchPlane_FailsToSelectPlane_ReturnsFalse method can be accessed from anywhere.
    • This is important, because xUnit test runner is external agent.
    • xUnit test runner need to have accessed to this SelectSketchPlane_FailsToSelectPlane_ReturnsFalse method.
    • Because of this requirment we need to give public accessor.
  • void :
    • This is the return type of SelectSketchPlane_FailsToSelectPlane_ReturnsFalse method.
    • Generally we donโ€™t return anything from test method.
    • Because of this we return void means we are not returning anything.
  • SelectSketchPlane_FailsToSelectPlane_ReturnsFalse
    • This is the name of method.
    • This name is created in 3 different parts and combined with underscore โ€œ_โ€œ.
    • Different parts are explained below ๐Ÿ‘‡๐Ÿป:
      • Part 1: Name of method we are testing.
      • Part 2: Return value we are expecting.
      • Part 3: Condition for which we are testing the method.
_mockSwDoc.Setup(d => d.Extension).Returns(_mockModelDocExtension.Object);
  • _mockSwDoc:
    • This is mocking object for ModelDoc2 object.
  • _mockSwDoc.Setup():
    • We are using the Setup() method from _mockSwDoc object.
    • This is important and most basic one you should learn.
    • This Setup() method allow us to setup the behavior of object it is mocking and its members/methods.
    • Please follow me and you will be able to understand how we setup the child members/methods.
  • Setup(d => d.Extension)
    • d is object we are mocking.
    • d represent ModelDoc2.
    • From this d object we will setup Extension property.
  • Returns(_mockModelDocExtension.Object)
    • Here are setting up return value of Extension property.
    • Above we are saying, whenever Extension property called, return ModelDocExtension object from _mockModelDocExtension.Object variable.
_mockModelDocExtension.Setup(e => e.SelectByID2("Right Plane", "PLANE", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault)).Returns(true);
  • _mockModelDocExtension:
    • This is mocking object for ModelDocExtension object.
  • _mockModelDocExtension.Setup():
    • We are using the Setup() method from _mockModelDocExtension object.
    • This is important and most basic one you should learn.
    • This Setup() method allow us to setup the behavior of object it is mocking and its members/methods.
    • Please follow me and you will be able to understand how we setup the child members/methods.
  • Setup(e => e.SelectByID2("Right Plane", "PLANE", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault))
    • e is object we are mocking.
    • e represent ModelDocExtension.
    • From this e object we will setup SelectByID2 method.
  • Returns(_mockSketchManager.Object)
    • Here are setting up return value of SelectByID2 method.
    • Above we are saying, whenever SelectByID2 method called, return true from _mockSketchManager.Object variable.
// Act
bool result = _viewModel.SelectSketchPlane(_mockSwApp.Object, _mockSwDoc.Object);

In above code, we are calling SelectSketchPlane method of _viewModel variable and store the return value into result variable.

  • _viewModel:
    • ViewModel variable whose method we want to test.
  • SelectSketchPlane:
    • Method we want to test.
  • SelectSketchPlane(_mockSwApp.Object, _mockSwDoc.Object)
    • Calling SelectSketchPlane method and passing parameters to method.
    • _mockSwApp.Object: SldWorks object from _mockSwApp variable.
    • _mockSwDoc.Object: ModelDoc2 object from _mockSwDoc variable.
// Assert
Assert.False(result);
Assert.Equal("Failed to select Right Plane.", _viewModel.messageToShow);
_mockSwApp.Verify(a => a.CloseAllDocuments(true), Times.Once);
_mockSwApp.Verify(a => a.ExitApp(), Times.Once);
  • Assert.False(result);:
    • We are checking value of result variable is false.
    • If return value is false then our asseertion is true.
  • Assert.Equal("Failed to select Right Plane.", _viewModel.messageToShow);
    • Similar to previous line, here we are checking value of _viewModel.messageToShow is equal to Failed to select Right Plane.
    • If both value are same then our asseertion is true.
  • _mockSwApp.Verify(a => a.CloseAllDocuments(true), Times.Once);:
    • Here we are verifying CloseAllDocuments() method is called 1 time only.
    • We need to do this to make sure our code did not run too much.
  • _mockSwApp.Verify(a => a.ExitApp(), Times.Once);:
    • Here we are verifying ExitApp() method is called 1 time only.
    • We need to do this to make sure our code did not run too much.

Before running test cases, we need to rebuild the Test project.

After rebuild, we see test cases in Test Explorer as shown in below ๐Ÿ‘‡๐Ÿป image.

test-cases-in-test-explorer

Now we run all test cases.

Please see below ๐Ÿ‘‡๐Ÿป image for reference.

final-result


This is it !!!

I hope my efforts will helpful to someone!

If you found anything to add or update, please let me know on my e-mail.

Hope this post helps you to Test [SelectSketchPlane] Method.

If you like the post then please share it with your friends also.

Do let me know by you like this post or not!

Till then, Happy learning!!!

Updated: