SOLIDWORKS C# API - Test CreatePartDocument Method
Objective
I want to:
- Test
CreatePartDocument
Method. - I will not explain each line, since they are already explained in previous articles.
- We will continue from previous article ๐ Test SelectSketchPlane Method.
Demo Video
Watch the video below to learn how to Test CreatePartDocument 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 bool CreatePartDocument(SldWorks.SldWorks swApp,out 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 ๐๐ป CreatePartDocument
method of
_viewModel
variable.
public bool CreatePartDocument(SldWorks.SldWorks swApp,out ModelDoc2 swDoc)
{
string defaultTemplate = swApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart);
if (string.IsNullOrEmpty(defaultTemplate))
{
messageToShow = "Default part template is empty.";
swDoc = null;
return false;
}
swDoc = swApp.NewDocument(defaultTemplate, 0, 0, 0);
if (swDoc == null)
{
messageToShow = "Failed to create new Part document.";
return false;
}
return SelectSketchPlane(swApp, swDoc);
}
To test CreatePartDocument
method, we
need 4 test cases.
- TestCase 1 : When Template Is Empty, Returns False
- TestCase 2 : When NewDocument Fails, Returns False
- TestCase 3 : When SketchPlane Selection Fails, Returns False
- TestCase 4 : When Document Create Successfully, Returns True
Before we add test cases, we will create a region called โTest Method [CreatePartDocument
]โ.
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 CreatePartDocument_WhenTemplateIsEmpty_ReturnsFalse()
{
// Arrange
_mockSwApp.Setup(a => a.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart))
.Returns(string.Empty);
// Act
bool result = _viewModel.CreatePartDocument(_mockSwApp.Object, out ModelDoc2 swDoc);
// Assert
Assert.False(result);
Assert.Null(swDoc);
Assert.Equal("Default part template is empty.", _viewModel.messageToShow);
}
Explanation of above CreatePartDocument_WhenTemplateIsEmpty_ReturnsFalse
is give below.
[Fact]
public void CreatePartDocument_WhenTemplateIsEmpty_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
CreatePartDocument_WhenTemplateIsEmpty_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 thisCreatePartDocument_WhenTemplateIsEmpty_ReturnsFalse
method.- Because of this requirment we need to give
public
accessor.
- This means that the
void
:- This is the return type of
CreatePartDocument_WhenTemplateIsEmpty_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
CreatePartDocument_WhenTemplateIsEmpty_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: Condition for which we are testing the method.
- Part 3: Return value we are expecting.
_mockSwApp.Setup(a => a.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart))
.Returns(string.Empty);
_mockSwApp
:- This is mocking object for
SldWorks.SldWorks
object.
- This is mocking object for
_mockSwApp.Setup()
:- We are using the
Setup()
method from_mockSwApp
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(a => a.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart))
a
is object we are mocking.a
representSldWorks.SldWorks
.- From this
a
object we will setupGetUserPreferenceStringValue
method. - This
GetUserPreferenceStringValue()
method, take a parameter.- Parameter Value:
(int)swUserPreferenceStringValue_e.swDefaultTemplatePart
- Parameter Value:
Returns(string.Empty)
- Here are setting up return value of
GetUserPreferenceStringValue
method. - Above we are saying, whenever
GetUserPreferenceStringValue
method called, returnstring.Empty
.
- Here are setting up return value of
// Act
bool result = _viewModel.CreatePartDocument(_mockSwApp.Object, out ModelDoc2 swDoc);
In above code, we are calling CreatePartDocument
method of
_viewModel
variable and
store the return value into result
variable.
_viewModel
:- ViewModel variable whose method we want to test.
CreatePartDocument
:- Method we want to test.
CreatePartDocument(_mockSwApp.Object, out ModelDoc2 swDoc)
- Calling
CreatePartDocument
method and passing parameters to method. _mockSwApp.Object
:SldWorks
object from_mockSwApp
variable._mockSwDoc.Object
:ModelDoc2
object from_mockSwDoc
variable, which we send out from this method.
- Calling
// Assert
Assert.False(result);
Assert.Null(swDoc);
Assert.Equal("Default part template is empty.", _viewModel.messageToShow);
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.Null(swDoc);
:- We are checking value of
swDoc
variable isnull
. - If value of
swDoc
variable isnull
then our asseertion is true.
- We are checking value of
Assert.Equal("Default part template is empty.", _viewModel.messageToShow);
:- Here we are checking value of
_viewModel.messageToShow
is equal to Default part template is empty. - If both value are same then our asseertion is true.
- Here we are checking value of
Add [TestCase 2]
In this section, we set up TestCase 2.
Please see below ๐๐ป code sample for set up.
[Fact]
public void CreatePartDocument_WhenNewDocumentFails_ReturnsFalse()
{
// Arrange
_mockSwApp.Setup(a => a.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart))
.Returns("defaultTemplatePath");
_mockSwApp.Setup(a => a.NewDocument("defaultTemplatePath", 0, 0, 0))
.Returns((ModelDoc2)null);
// Act
bool result = _viewModel.CreatePartDocument(_mockSwApp.Object, out ModelDoc2 swDoc);
// Assert
Assert.False(result);
Assert.Null(swDoc);
Assert.Equal("Failed to create new Part document.", _viewModel.messageToShow);
}
Explanation of above CreatePartDocument_WhenNewDocumentFails_ReturnsFalse
is give below.
[Fact]
public void CreatePartDocument_WhenNewDocumentFails_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
CreatePartDocument_WhenNewDocumentFails_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 thisCreatePartDocument_WhenNewDocumentFails_ReturnsFalse
method.- Because of this requirment we need to give
public
accessor.
- This means that the
void
:- This is the return type of
CreatePartDocument_WhenNewDocumentFails_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
CreatePartDocument_WhenNewDocumentFails_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: Condition for which we are testing the method.
- Part 3: Return value we are expecting.
_mockSwApp.Setup(a => a.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart))
.Returns("defaultTemplatePath");
_mockSwApp
:- This is mocking object for
SldWorks.SldWorks
object.
- This is mocking object for
_mockSwApp.Setup()
:- We are using the
Setup()
method from_mockSwApp
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(a => a.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart))
a
is object we are mocking.a
representSldWorks.SldWorks
.- From this
a
object we will setupGetUserPreferenceStringValue
method. - This
GetUserPreferenceStringValue()
method, take a parameter.- Parameter Value:
(int)swUserPreferenceStringValue_e.swDefaultTemplatePart
- Parameter Value:
Returns("defaultTemplatePath")
- Here are setting up return value of
GetUserPreferenceStringValue
method. - Above we are saying, whenever
GetUserPreferenceStringValue
method called, returndefaultTemplatePath
asstring
.
- Here are setting up return value of
_mockSwApp.Setup(a => a.NewDocument("defaultTemplatePath", 0, 0, 0))
.Returns((ModelDoc2)null);
_mockSwApp
:- This is mocking object for
SldWorks.SldWorks
object.
- This is mocking object for
_mockSwApp.Setup()
:- We are using the
Setup()
method from_mockSwApp
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(a => a.NewDocument("defaultTemplatePath", 0, 0, 0))
a
is object we are mocking.a
representSldWorks.SldWorks
.- From this
a
object we will setupNewDocument
method. - This
NewDocument
method, takes 4 parameters.- TemplateName, PaperSize, Width, Height
Returns((ModelDoc2)null)
- Here are setting up return value of
NewDocument
method. - Above we are saying, whenever
NewDocument
method called, return(ModelDoc2)null
variable.
- Here are setting up return value of
// Act
bool result = _viewModel.CreatePartDocument(_mockSwApp.Object, out ModelDoc2 swDoc);
In above code, we are calling CreatePartDocument
method of
_viewModel
variable and
store the return value into result
variable.
_viewModel
:- ViewModel variable whose method we want to test.
CreatePartDocument
:- Method we want to test.
CreatePartDocument(_mockSwApp.Object, out ModelDoc2 swDoc)
- Calling
CreatePartDocument
method and passing parameters to method. _mockSwApp.Object
:SldWorks
object from_mockSwApp
variable._mockSwDoc.Object
:ModelDoc2
object from_mockSwDoc
variable, which we send out from this method.
- Calling
// Assert
Assert.False(result);
Assert.Null(swDoc);
Assert.Equal("Failed to create new Part document.", _viewModel.messageToShow);
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.Null(swDoc);
:- We are checking value of
swDoc
variable isnull
. - If value of
swDoc
variable isnull
then our asseertion is true.
- We are checking value of
Assert.Equal("Failed to create new Part document.", _viewModel.messageToShow);
:- Here we are checking value of
_viewModel.messageToShow
is equal to Failed to create new Part document. - If both value are same then our asseertion is true.
- Here we are checking value of
Add [TestCase 3]
In this section, we set up TestCase 3.
Please see below ๐๐ป code sample for set up.
[Fact]
public void CreatePartDocument_FailedSketchPlaneSelection_ReturnsFalse()
{
// Arrange
_mockSwApp.Setup(app => app.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart))
.Returns("DefaultTemplatePath");
_mockSwApp.Setup(app => app.NewDocument("DefaultTemplatePath", 0, 0, 0))
.Returns(_mockSwDoc.Object);
// Mock SelectSketchPlane to return false, simulating failure
var viewModelMock = new Mock<MainWindowViewModel>(_mockEventAggregator.Object, _mockContainer.Object, _mockConversionHelper.Object) { CallBase = true };
viewModelMock.Setup(vm => vm.SelectSketchPlane(_mockSwApp.Object, _mockSwDoc.Object))
.Returns(false);
// Act
var result = viewModelMock.Object.CreatePartDocument(_mockSwApp.Object, out var createdDoc);
// Assert
Assert.False(result);
Assert.Equal(_mockSwDoc.Object, createdDoc);
}
Explanation of above CreatePartDocument_FailedSketchPlaneSelection_ReturnsFalse
is give below.
[Fact]
public void CreatePartDocument_FailedSketchPlaneSelection_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
CreatePartDocument_FailedSketchPlaneSelection_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 thisCreatePartDocument_FailedSketchPlaneSelection_ReturnsFalse
method.- Because of this requirment we need to give
public
accessor.
- This means that the
void
:- This is the return type of
CreatePartDocument_FailedSketchPlaneSelection_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
CreatePartDocument_FailedSketchPlaneSelection_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: Condition for which we are testing the method.
- Part 3: Return value we are expecting.
_mockSwApp.Setup(a => a.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart))
.Returns("defaultTemplatePath");
_mockSwApp
:- This is mocking object for
SldWorks.SldWorks
object.
- This is mocking object for
_mockSwApp.Setup()
:- We are using the
Setup()
method from_mockSwApp
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(a => a.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart))
a
is object we are mocking.a
representSldWorks.SldWorks
.- From this
a
object we will setupGetUserPreferenceStringValue
method. - This
GetUserPreferenceStringValue()
method, take a parameter.- Parameter Value:
(int)swUserPreferenceStringValue_e.swDefaultTemplatePart
- Parameter Value:
Returns("defaultTemplatePath")
- Here are setting up return value of
GetUserPreferenceStringValue
method. - Above we are saying, whenever
GetUserPreferenceStringValue
method called, returndefaultTemplatePath
asstring
.
- Here are setting up return value of
_mockSwApp.Setup(a => a.NewDocument("defaultTemplatePath", 0, 0, 0))
.Returns(_mockSwDoc.Object);
_mockSwApp
:- This is mocking object for
SldWorks.SldWorks
object.
- This is mocking object for
_mockSwApp.Setup()
:- We are using the
Setup()
method from_mockSwApp
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(a => a.NewDocument("defaultTemplatePath", 0, 0, 0)
a
is object we are mocking.a
representSldWorks.SldWorks
.- From this
a
object we will setupNewDocument
method. - This
NewDocument
method, takes 4 parameters.- TemplateName, PaperSize, Width, Height
Returns(_mockSwDoc.Object)
- Here are setting up return value of
NewDocument
method. - Above we are saying, whenever
NewDocument
method called, return_mockSwDoc.Object
variable.
- Here are setting up return value of
var viewModelMock = new Mock<MainWindowViewModel>(_mockEventAggregator.Object, _mockContainer.Object, _mockConversionHelper.Object) { CallBase = true };
- In above line, we create a new Moq variable.
- This new variable mock,
MainWindowViewModel
class, which we use next.
viewModelMock.Setup(vm => vm.SelectSketchPlane(_mockSwApp.Object, _mockSwDoc.Object))
.Returns(false);
viewModelMock
:- This is mocking object for
MainWindowViewModel
object.
- This is mocking object for
viewModelMock.Setup()
:- We are using the
Setup()
method fromviewModelMock
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(vm => vm.SelectSketchPlane(_mockSwApp.Object, _mockSwDoc.Object))
vm
is object we are mocking.vm
representMainWindowViewModel
.- From this
vm
object we will setupSelectSketchPlane
method.
Returns(false)
- Here are setting up return value of
SelectSketchPlane
method. - Above we are saying, whenever
SelectSketchPlane
method called, returnfalse
value.
- Here are setting up return value of
// Act
var result = viewModelMock.Object.CreatePartDocument(_mockSwApp.Object, out var createdDoc);
In above code, we are calling CreatePartDocument
method of
viewModelMock.Object
variable and store the return value into result
variable.
viewModelMock.Object
:- ViewModel variable whose method we want to test.
CreatePartDocument
:- Method we want to test.
CreatePartDocument(_mockSwApp.Object, out ModelDoc2 swDoc)
- Calling
CreatePartDocument
method and passing parameters to method. _mockSwApp.Object
:SldWorks
object from_mockSwApp
variable._mockSwDoc.Object
:ModelDoc2
object from_mockSwDoc
variable, which we send out from this method.
- Calling
// Assert
Assert.False(result);
Assert.Null(swDoc);
Assert.Equal("Failed to create new Part document.", _viewModel.messageToShow);
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(_mockSwDoc.Object, createdDoc);
:- Here we are checking value of
_mockSwDoc.Object
is equal tocreatedDoc
- If both value are same then our asseertion is true.
- Here we are checking value of
Add [TestCase 4]
In this section, we set up TestCase 4.
Please see below ๐๐ป code sample for set up.
[Fact]
public void CreatePartDocument_WhenDocumentCreatedSuccessfully_ReturnsTrue()
{
// Arrange
var mockDoc = new Mock<ModelDoc2>();
_mockSwApp.Setup(a => a.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart))
.Returns("defaultTemplatePath");
_mockSwApp.Setup(a => a.NewDocument("defaultTemplatePath", 0, 0, 0))
.Returns(mockDoc.Object);
// Mock the SelectSketchPlane method to return true
var viewModel = new Mock<MainWindowViewModel>(_mockEventAggregator.Object, _mockContainer.Object, _mockConversionHelper.Object) { CallBase = true };
viewModel.Setup(vm => vm.SelectSketchPlane(_mockSwApp.Object, mockDoc.Object)).Returns(true);
// Act
bool result = viewModel.Object.CreatePartDocument(_mockSwApp.Object, out ModelDoc2 swDoc);
// Assert
Assert.True(result);
Assert.NotNull(swDoc);
}
Explanation of above CreatePartDocument_WhenDocumentCreatedSuccessfully_ReturnsTrue
is give below.
[Fact]
public void CreatePartDocument_WhenDocumentCreatedSuccessfully_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
CreatePartDocument_WhenDocumentCreatedSuccessfully_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 thisCreatePartDocument_WhenDocumentCreatedSuccessfully_ReturnsTrue
method.- Because of this requirment we need to give
public
accessor.
- This means that the
void
:- This is the return type of
CreatePartDocument_WhenDocumentCreatedSuccessfully_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
CreatePartDocument_WhenDocumentCreatedSuccessfully_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: Condition for which we are testing the method.
- Part 3: Return value we are expecting.
_mockSwApp.Setup(a => a.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart))
.Returns("defaultTemplatePath");
_mockSwApp
:- This is mocking object for
SldWorks.SldWorks
object.
- This is mocking object for
_mockSwApp.Setup()
:- We are using the
Setup()
method from_mockSwApp
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(a => a.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart))
a
is object we are mocking.a
representSldWorks.SldWorks
.- From this
a
object we will setupGetUserPreferenceStringValue
method. - This
GetUserPreferenceStringValue()
method, take a parameter.- Parameter Value:
(int)swUserPreferenceStringValue_e.swDefaultTemplatePart
- Parameter Value:
Returns("defaultTemplatePath")
- Here are setting up return value of
GetUserPreferenceStringValue
method. - Above we are saying, whenever
GetUserPreferenceStringValue
method called, returndefaultTemplatePath
asstring
.
- Here are setting up return value of
_mockSwApp.Setup(a => a.NewDocument("defaultTemplatePath", 0, 0, 0))
.Returns(_mockSwDoc.Object);
_mockSwApp
:- This is mocking object for
SldWorks.SldWorks
object.
- This is mocking object for
_mockSwApp.Setup()
:- We are using the
Setup()
method from_mockSwApp
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(a => a.NewDocument("defaultTemplatePath", 0, 0, 0)
a
is object we are mocking.a
representSldWorks.SldWorks
.- From this
a
object we will setupNewDocument
method. - This
NewDocument
method, takes 4 parameters.- TemplateName, PaperSize, Width, Height
Returns(_mockSwDoc.Object)
- Here are setting up return value of
NewDocument
method. - Above we are saying, whenever
NewDocument
method called, return_mockSwDoc.Object
variable.
- Here are setting up return value of
var viewModelMock = new Mock<MainWindowViewModel>(_mockEventAggregator.Object, _mockContainer.Object, _mockConversionHelper.Object) { CallBase = true };
- In above line, we create a new Moq variable.
- This new variable mock,
MainWindowViewModel
class, which we use next.
viewModelMock.Setup(vm => vm.SelectSketchPlane(_mockSwApp.Object, _mockSwDoc.Object))
.Returns(true);
viewModelMock
:- This is mocking object for
MainWindowViewModel
object.
- This is mocking object for
viewModelMock.Setup()
:- We are using the
Setup()
method fromviewModelMock
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(vm => vm.SelectSketchPlane(_mockSwApp.Object, _mockSwDoc.Object))
vm
is object we are mocking.vm
representMainWindowViewModel
.- From this
vm
object we will setupSelectSketchPlane
method.
Returns(false)
- Here are setting up return value of
SelectSketchPlane
method. - Above we are saying, whenever
SelectSketchPlane
method called, returnfalse
value.
- Here are setting up return value of
// Act
bool result = viewModel.Object.CreatePartDocument(_mockSwApp.Object, out ModelDoc2 swDoc);
In above code, we are calling CreatePartDocument
method of
viewModelMock.Object
variable and store the return value into result
variable.
viewModelMock.Object
:- ViewModel variable whose method we want to test.
CreatePartDocument
:- Method we want to test.
CreatePartDocument(_mockSwApp.Object, out ModelDoc2 swDoc)
- Calling
CreatePartDocument
method and passing parameters to method. _mockSwApp.Object
:SldWorks
object from_mockSwApp
variable._mockSwDoc.Object
:ModelDoc2
object from_mockSwDoc
variable, which we send out from this method.
- Calling
// Assert
Assert.True(result);
Assert.NotNull(swDoc);
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
Assert.NotNull(swDoc);
:- We are checking value of
swDoc
variable is notnull
. - If value of
swDoc
variable is notnull
then our asseertion is true.
- We are checking value of
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 [CreatePartDocument
] 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!!!