SOLIDWORKS C# API - Test SelectSketchPlane Method
Objective
I want to:
- Test SelectSketchPlaneMethod.
- 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.
- TestCase 1 : When Plane selection Succeeds Returns True
- 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
                            xUnittest runner recognizes.
 
- public:- This means that the SelectSketchPlane_SuccessfullySelectsPlane_ReturnsTruemethod can be accessed from anywhere.
- This is important, because xUnittest runner is external agent.
- xUnittest runner need to have accessed to this- SelectSketchPlane_SuccessfullySelectsPlane_ReturnsTruemethod.
- Because of this requirment we need to give publicaccessor.
 
- This means that the 
- void:- This is the return type of SelectSketchPlane_SuccessfullySelectsPlane_ReturnsTruemethod.
- Generally we donโt return anything from test method.
- Because of this we return voidmeans we are not returning anything.
 
- This is the return type of 
- 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 ModelDoc2object.
 
- This is mocking object for 
- _mockSwDoc.Setup():- We are using the Setup()method from_mockSwDocobject.
- 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.
 
- We are using the 
- Setup(d => d.Extension)- dis object we are mocking.
- drepresent- ModelDoc2.
- From this dobject we will setupExtensionproperty.
 
- Returns(_mockModelDocExtension.Object)- Here are setting up return value of Extensionproperty.
- Above we are saying, whenever Extensionproperty called, returnModelDocExtensionobject from_mockModelDocExtension.Objectvariable.
 
- Here are setting up return value of 
_mockSwDoc.Setup(d => d.SketchManager).Returns(_mockSketchManager.Object);
- _mockSwDoc:- This is mocking object for ModelDoc2object.
 
- This is mocking object for 
- _mockSwDoc.Setup():- We are using the Setup()method from_mockSwDocobject.
- 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.
 
- We are using the 
- Setup(d => d.SketchManager)- dis object we are mocking.
- drepresent- ModelDoc2.
- From this dobject we will setupSketchManagerproperty.
 
- Returns(_mockSketchManager.Object)- Here are setting up return value of SketchManagerproperty.
- Above we are saying, whenever SketchManagerproperty called, returnSketchManagerobject from_mockSketchManager.Objectvariable.
 
- Here are setting up return value of 
_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 ModelDocExtensionobject.
 
- This is mocking object for 
- _mockModelDocExtension.Setup():- We are using the Setup()method from_mockModelDocExtensionobject.
- 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.
 
- We are using the 
- Setup(e => e.SelectByID2("Right Plane", "PLANE", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault))- eis object we are mocking.
- erepresent- ModelDocExtension.
- From this eobject we will setupSelectByID2method.
 
- Returns(_mockSketchManager.Object)- Here are setting up return value of SelectByID2method.
- Above we are saying, whenever SelectByID2method called, returntruefrom_mockSketchManager.Objectvariable.
 
- Here are setting up return value of 
_mockSwDoc.Setup(d => d.LengthUnit).Returns((int)swLengthUnit_e.swINCHES);
- _mockSwDoc:- This is mocking object for ModelDoc2object.
 
- This is mocking object for 
- _mockSwDoc.Setup():- We are using the Setup()method from_mockSwDocobject.
- 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.
 
- We are using the 
- Setup(d => d.LengthUnit)- dis object we are mocking.
- drepresent- ModelDoc2.
- From this dobject we will setupLengthUnitproperty.
 
- Returns((int)swLengthUnit_e.swINCHES)- Here are setting up return value of LengthUnitproperty.
- Above we are saying, whenever LengthUnitproperty called, return(int)swLengthUnit_e.swINCHES)from_mockSketchManager.Objectvariable.
 
- Here are setting up return value of 
_mockConversionHelper.Setup(c => c.UnitConversion(It.IsAny<swLengthUnit_e>()));
- _mockConversionHelper:- This is mocking object for IUnitConversionHelperobject.
 
- This is mocking object for 
- _mockConversionHelper.Setup():- We are using the Setup()method from_mockConversionHelperobject.
- 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.
 
- We are using the 
- Setup(c => c.UnitConversion(It.IsAny<swLengthUnit_e>()))- cis object we are mocking.
- crepresent- IUnitConversionHelper.
- From this cobject we will setupUnitConversionmethod.
- This UnitConversionmethod will take any value ofIt.IsAny<swLengthUnit_e>().
- This time we did not Return any value, we just want to setup
                            UnitConversionmethod.
 
_mockConversionHelper.Setup(c => c.LengthConversionFactor).Returns(0.0254);
- _mockConversionHelper:- This is mocking object for ModelDoc2object.
 
- This is mocking object for 
- _mockConversionHelper.Setup():- We are using the Setup()method from_mockSwDocobject.
- 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.
 
- We are using the 
- Setup(c => c.LengthConversionFactor)- cis object we are mocking.
- crepresent- IUnitConversionHelper.
- From this cobject we will setupLengthConversionFactorproperty.
 
- Returns(0.0254)- Here are setting up return value of LengthConversionFactorproperty.
- Above we are saying, whenever LengthConversionFactorproperty called, return0.0254.
 
- Here are setting up return value of 
_mockEndPoint.Setup(p => p.XPoint).Returns(2);
- _mockEndPoint:- This is mocking object for IPointViewModelobject.
 
- This is mocking object for 
- _mockEndPoint.Setup():- We are using the Setup()method from_mockEndPointobject.
- 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.
 
- We are using the 
- Setup(p => p.XPoint)- pis object we are mocking.
- prepresent- IPointViewModel.
- From this pobject we will setupXPointproperty.
 
- Returns(2)- Here are setting up return value of XPointproperty.
- Above we are saying, whenever XPointproperty called, return2.
 
- Here are setting up return value of 
_mockEndPoint.Setup(p => p.YPoint).Returns(3);
- _mockEndPoint:- This is mocking object for IPointViewModelobject.
 
- This is mocking object for 
- _mockEndPoint.Setup():- We are using the Setup()method from_mockEndPointobject.
- 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.
 
- We are using the 
- Setup(p => p.YPoint)- pis object we are mocking.
- prepresent- IPointViewModel.
- From this pobject we will setupYPointproperty.
 
- Returns(3)- Here are setting up return value of YPointproperty.
- Above we are saying, whenever YPointproperty called, return3.
 
- Here are setting up return value of 
_mockEndPoint.Setup(p => p.ZPoint).Returns(3);
- _mockEndPoint:- This is mocking object for IPointViewModelobject.
 
- This is mocking object for 
- _mockEndPoint.Setup():- We are using the Setup()method from_mockEndPointobject.
- 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.
 
- We are using the 
- Setup(p => p.ZPoint)- pis object we are mocking.
- prepresent- IPointViewModel.
- From this pobject we will setupZPointproperty.
 
- Returns(0)- Here are setting up return value of ZPointproperty.
- Above we are saying, whenever ZPointproperty called, return0.
 
- Here are setting up return value of 
// 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 SketchManagerobject.
 
- This is mocking object for 
- _mockSketchManager.Setup():- We are using the Setup()method from_mockSketchManagerobject.
- 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.
 
 
- We are using the 
- Setup(sm => sm.CreateLine(It.IsAny<double>(), It.IsAny<double>(), It.IsAny<double>(), It.IsAny<double>(), It.IsAny<double>(), It.IsAny<double>()))- smis object we are mocking.
- smrepresent- SketchManager.
- From this smobject we will setupCreateLinemethod.
- 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 CreateLinemethod.
- Above we are saying, whenever CreateLinemethod called, returnSketchSegmentobject from_mockSketchSegment.Objectvariable.
 
- Here are setting up return value of 
// 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 SelectSketchPlanemethod and passing parameters to method.
- _mockSwApp.Object:- SldWorksobject from- _mockSwAppvariable.
- _mockSwDoc.Object:- ModelDoc2object from- _mockSwDocvariable.
 
- Calling 
// 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 resultvariable istrue.
- If return value is truethen our asseertion is true.
 
- We are checking value of 
- _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.
 
- Here we are verifying 
- _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.
 
- Here we are verifying 
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
                            xUnittest runner recognizes.
 
- public:- This means that the SelectSketchPlane_FailsToSelectPlane_ReturnsFalsemethod can be accessed from anywhere.
- This is important, because xUnittest runner is external agent.
- xUnittest runner need to have accessed to this- SelectSketchPlane_FailsToSelectPlane_ReturnsFalsemethod.
- Because of this requirment we need to give publicaccessor.
 
- This means that the 
- void:- This is the return type of SelectSketchPlane_FailsToSelectPlane_ReturnsFalsemethod.
- Generally we donโt return anything from test method.
- Because of this we return voidmeans we are not returning anything.
 
- This is the return type of 
- 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 ModelDoc2object.
 
- This is mocking object for 
- _mockSwDoc.Setup():- We are using the Setup()method from_mockSwDocobject.
- 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.
 
- We are using the 
- Setup(d => d.Extension)- dis object we are mocking.
- drepresent- ModelDoc2.
- From this dobject we will setupExtensionproperty.
 
- Returns(_mockModelDocExtension.Object)- Here are setting up return value of Extensionproperty.
- Above we are saying, whenever Extensionproperty called, returnModelDocExtensionobject from_mockModelDocExtension.Objectvariable.
 
- Here are setting up return value of 
_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 ModelDocExtensionobject.
 
- This is mocking object for 
- _mockModelDocExtension.Setup():- We are using the Setup()method from_mockModelDocExtensionobject.
- 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.
 
- We are using the 
- Setup(e => e.SelectByID2("Right Plane", "PLANE", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault))- eis object we are mocking.
- erepresent- ModelDocExtension.
- From this eobject we will setupSelectByID2method.
 
- Returns(_mockSketchManager.Object)- Here are setting up return value of SelectByID2method.
- Above we are saying, whenever SelectByID2method called, returntruefrom_mockSketchManager.Objectvariable.
 
- Here are setting up return value of 
// 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 SelectSketchPlanemethod and passing parameters to method.
- _mockSwApp.Object:- SldWorksobject from- _mockSwAppvariable.
- _mockSwDoc.Object:- ModelDoc2object from- _mockSwDocvariable.
 
- Calling 
// 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 resultvariable isfalse.
- If return value is falsethen our asseertion is true.
 
- We are checking value of 
- Assert.Equal("Failed to select Right Plane.", _viewModel.messageToShow);- Similar to previous line, here we are checking value of _viewModel.messageToShowis equal to Failed to select Right Plane.
- If both value are same then our asseertion is true.
 
- Similar to previous line, here we are checking value of 
- _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.
 
- Here we are verifying 
- _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.
 
- Here we are verifying 
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.
Now we run all test cases.
Please see below ๐๐ป image for reference.
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!!!

