SOLIDWORKS C# API - Test SelectSketchPlane Method
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.
- 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
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 thisSelectSketchPlane_SuccessfullySelectsPlane_ReturnsTrue
method.- Because of this requirment we need to give
public
accessor.
- This means that the
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.
- 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
ModelDoc2
object.
- This is mocking object for
_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.
- We are using the
Setup(d => d.Extension)
d
is object we are mocking.d
representModelDoc2
.- From this
d
object we will setupExtension
property.
Returns(_mockModelDocExtension.Object)
- Here are setting up return value of
Extension
property. - Above we are saying, whenever
Extension
property called, returnModelDocExtension
object from_mockModelDocExtension.Object
variable.
- Here are setting up return value of
_mockSwDoc.Setup(d => d.SketchManager).Returns(_mockSketchManager.Object);
_mockSwDoc
:- This is mocking object for
ModelDoc2
object.
- This is mocking object for
_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.
- We are using the
Setup(d => d.SketchManager)
d
is object we are mocking.d
representModelDoc2
.- From this
d
object we will setupSketchManager
property.
Returns(_mockSketchManager.Object)
- Here are setting up return value of
SketchManager
property. - Above we are saying, whenever
SketchManager
property called, returnSketchManager
object from_mockSketchManager.Object
variable.
- 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
ModelDocExtension
object.
- This is mocking object for
_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.
- We are using the
Setup(e => e.SelectByID2("Right Plane", "PLANE", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault))
e
is object we are mocking.e
representModelDocExtension
.- From this
e
object we will setupSelectByID2
method.
Returns(_mockSketchManager.Object)
- Here are setting up return value of
SelectByID2
method. - Above we are saying, whenever
SelectByID2
method called, returntrue
from_mockSketchManager.Object
variable.
- Here are setting up return value of
_mockSwDoc.Setup(d => d.LengthUnit).Returns((int)swLengthUnit_e.swINCHES);
_mockSwDoc
:- This is mocking object for
ModelDoc2
object.
- This is mocking object for
_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.
- We are using the
Setup(d => d.LengthUnit)
d
is object we are mocking.d
representModelDoc2
.- From this
d
object we will setupLengthUnit
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.
- Here are setting up return value of
_mockConversionHelper.Setup(c => c.UnitConversion(It.IsAny<swLengthUnit_e>()));
_mockConversionHelper
:- This is mocking object for
IUnitConversionHelper
object.
- This is mocking object for
_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.
- We are using the
Setup(c => c.UnitConversion(It.IsAny<swLengthUnit_e>()))
c
is object we are mocking.c
representIUnitConversionHelper
.- From this
c
object we will setupUnitConversion
method. - This
UnitConversion
method will take any value ofIt.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.
- This is mocking object for
_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.
- We are using the
Setup(c => c.LengthConversionFactor)
c
is object we are mocking.c
representIUnitConversionHelper
.- From this
c
object we will setupLengthConversionFactor
property.
Returns(0.0254)
- Here are setting up return value of
LengthConversionFactor
property. - Above we are saying, whenever
LengthConversionFactor
property called, return0.0254
.
- Here are setting up return value of
_mockEndPoint.Setup(p => p.XPoint).Returns(2);
_mockEndPoint
:- This is mocking object for
IPointViewModel
object.
- This is mocking object for
_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.
- We are using the
Setup(p => p.XPoint)
p
is object we are mocking.p
representIPointViewModel
.- From this
p
object we will setupXPoint
property.
Returns(2)
- Here are setting up return value of
XPoint
property. - Above we are saying, whenever
XPoint
property called, return2
.
- Here are setting up return value of
_mockEndPoint.Setup(p => p.YPoint).Returns(3);
_mockEndPoint
:- This is mocking object for
IPointViewModel
object.
- This is mocking object for
_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.
- We are using the
Setup(p => p.YPoint)
p
is object we are mocking.p
representIPointViewModel
.- From this
p
object we will setupYPoint
property.
Returns(3)
- Here are setting up return value of
YPoint
property. - Above we are saying, whenever
YPoint
property called, return3
.
- Here are setting up return value of
_mockEndPoint.Setup(p => p.ZPoint).Returns(3);
_mockEndPoint
:- This is mocking object for
IPointViewModel
object.
- This is mocking object for
_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.
- We are using the
Setup(p => p.ZPoint)
p
is object we are mocking.p
representIPointViewModel
.- From this
p
object we will setupZPoint
property.
Returns(0)
- Here are setting up return value of
ZPoint
property. - Above we are saying, whenever
ZPoint
property 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
SketchManager
object.
- This is mocking object for
_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.
- 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>()))
sm
is object we are mocking.sm
representSketchManager
.- From this
sm
object we will setupCreateLine
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, returnSketchSegment
object from_mockSketchSegment.Object
variable.
- 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
SelectSketchPlane
method and passing parameters to method. _mockSwApp.Object
:SldWorks
object from_mockSwApp
variable._mockSwDoc.Object
:ModelDoc2
object from_mockSwDoc
variable.
- 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
result
variable istrue
. - If return value is
true
then 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
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 thisSelectSketchPlane_FailsToSelectPlane_ReturnsFalse
method.- Because of this requirment we need to give
public
accessor.
- This means that the
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.
- 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
ModelDoc2
object.
- This is mocking object for
_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.
- We are using the
Setup(d => d.Extension)
d
is object we are mocking.d
representModelDoc2
.- From this
d
object we will setupExtension
property.
Returns(_mockModelDocExtension.Object)
- Here are setting up return value of
Extension
property. - Above we are saying, whenever
Extension
property called, returnModelDocExtension
object from_mockModelDocExtension.Object
variable.
- 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
ModelDocExtension
object.
- This is mocking object for
_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.
- We are using the
Setup(e => e.SelectByID2("Right Plane", "PLANE", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault))
e
is object we are mocking.e
representModelDocExtension
.- From this
e
object we will setupSelectByID2
method.
Returns(_mockSketchManager.Object)
- Here are setting up return value of
SelectByID2
method. - Above we are saying, whenever
SelectByID2
method called, returntrue
from_mockSketchManager.Object
variable.
- 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
SelectSketchPlane
method and passing parameters to method. _mockSwApp.Object
:SldWorks
object from_mockSwApp
variable._mockSwDoc.Object
:ModelDoc2
object from_mockSwDoc
variable.
- 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
result
variable isfalse
. - If return value is
false
then 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.messageToShow
is 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!!!