SOLIDWORKS C# API - Test CreatePartDocument Method

14 minute read

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.

  1. TestCase 1 : When Template Is Empty, Returns False
  2. TestCase 2 : When NewDocument Fails, Returns False
  3. TestCase 3 : When SketchPlane Selection Fails, Returns False
  4. 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 this CreatePartDocument_WhenTemplateIsEmpty_ReturnsFalse method.
    • Because of this requirment we need to give public accessor.
  • 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.
  • 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.
  • _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.
  • Setup(a => a.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart))
    • a is object we are mocking.
    • a represent SldWorks.SldWorks.
    • From this a object we will setup GetUserPreferenceStringValue method.
    • This GetUserPreferenceStringValue() method, take a parameter.
      • Parameter Value: (int)swUserPreferenceStringValue_e.swDefaultTemplatePart
  • Returns(string.Empty)
    • Here are setting up return value of GetUserPreferenceStringValue method.
    • Above we are saying, whenever GetUserPreferenceStringValue method called, return string.Empty.
// 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.
// 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 is false.
    • If return value is false then our asseertion is true.
  • Assert.Null(swDoc);:
    • We are checking value of swDoc variable is null.
    • If value of swDoc variable is null then our asseertion is true.
  • 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.

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 this CreatePartDocument_WhenNewDocumentFails_ReturnsFalse method.
    • Because of this requirment we need to give public accessor.
  • 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.
  • 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.
  • _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.
  • Setup(a => a.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart))
    • a is object we are mocking.
    • a represent SldWorks.SldWorks.
    • From this a object we will setup GetUserPreferenceStringValue method.
    • This GetUserPreferenceStringValue() method, take a parameter.
      • Parameter Value: (int)swUserPreferenceStringValue_e.swDefaultTemplatePart
  • Returns("defaultTemplatePath")
    • Here are setting up return value of GetUserPreferenceStringValue method.
    • Above we are saying, whenever GetUserPreferenceStringValue method called, return defaultTemplatePath as string.
_mockSwApp.Setup(a => a.NewDocument("defaultTemplatePath", 0, 0, 0))
          .Returns((ModelDoc2)null);
  • _mockSwApp:
    • This is mocking object for SldWorks.SldWorks object.
  • _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.
  • Setup(a => a.NewDocument("defaultTemplatePath", 0, 0, 0))
    • a is object we are mocking.
    • a represent SldWorks.SldWorks.
    • From this a object we will setup NewDocument 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.
// 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.
// 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 is false.
    • If return value is false then our asseertion is true.
  • Assert.Null(swDoc);:
    • We are checking value of swDoc variable is null.
    • If value of swDoc variable is null then our asseertion is true.
  • 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.

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 this CreatePartDocument_FailedSketchPlaneSelection_ReturnsFalse method.
    • Because of this requirment we need to give public accessor.
  • 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.
  • 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.
  • _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.
  • Setup(a => a.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart))
    • a is object we are mocking.
    • a represent SldWorks.SldWorks.
    • From this a object we will setup GetUserPreferenceStringValue method.
    • This GetUserPreferenceStringValue() method, take a parameter.
      • Parameter Value: (int)swUserPreferenceStringValue_e.swDefaultTemplatePart
  • Returns("defaultTemplatePath")
    • Here are setting up return value of GetUserPreferenceStringValue method.
    • Above we are saying, whenever GetUserPreferenceStringValue method called, return defaultTemplatePath as string.
_mockSwApp.Setup(a => a.NewDocument("defaultTemplatePath", 0, 0, 0))
          .Returns(_mockSwDoc.Object);
  • _mockSwApp:
    • This is mocking object for SldWorks.SldWorks object.
  • _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.
  • Setup(a => a.NewDocument("defaultTemplatePath", 0, 0, 0)
    • a is object we are mocking.
    • a represent SldWorks.SldWorks.
    • From this a object we will setup NewDocument 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.
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.
  • viewModelMock.Setup():
    • We are using the Setup() method from viewModelMock 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(vm => vm.SelectSketchPlane(_mockSwApp.Object, _mockSwDoc.Object))
    • vm is object we are mocking.
    • vm represent MainWindowViewModel.
    • From this vm object we will setup SelectSketchPlane method.
  • Returns(false)
    • Here are setting up return value of SelectSketchPlane method.
    • Above we are saying, whenever SelectSketchPlane method called, return false value.
// 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.
// 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 is false.
    • If return value is false then our asseertion is true.
  • Assert.Equal(_mockSwDoc.Object, createdDoc);:
    • Here we are checking value of _mockSwDoc.Object is equal to createdDoc
    • If both value are same then our asseertion is true.

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 this CreatePartDocument_WhenDocumentCreatedSuccessfully_ReturnsTrue method.
    • Because of this requirment we need to give public accessor.
  • 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.
  • 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.
  • _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.
  • Setup(a => a.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart))
    • a is object we are mocking.
    • a represent SldWorks.SldWorks.
    • From this a object we will setup GetUserPreferenceStringValue method.
    • This GetUserPreferenceStringValue() method, take a parameter.
      • Parameter Value: (int)swUserPreferenceStringValue_e.swDefaultTemplatePart
  • Returns("defaultTemplatePath")
    • Here are setting up return value of GetUserPreferenceStringValue method.
    • Above we are saying, whenever GetUserPreferenceStringValue method called, return defaultTemplatePath as string.
_mockSwApp.Setup(a => a.NewDocument("defaultTemplatePath", 0, 0, 0))
          .Returns(_mockSwDoc.Object);
  • _mockSwApp:
    • This is mocking object for SldWorks.SldWorks object.
  • _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.
  • Setup(a => a.NewDocument("defaultTemplatePath", 0, 0, 0)
    • a is object we are mocking.
    • a represent SldWorks.SldWorks.
    • From this a object we will setup NewDocument 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.
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.
  • viewModelMock.Setup():
    • We are using the Setup() method from viewModelMock 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(vm => vm.SelectSketchPlane(_mockSwApp.Object, _mockSwDoc.Object))
    • vm is object we are mocking.
    • vm represent MainWindowViewModel.
    • From this vm object we will setup SelectSketchPlane method.
  • Returns(false)
    • Here are setting up return value of SelectSketchPlane method.
    • Above we are saying, whenever SelectSketchPlane method called, return false value.
// 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.
// Assert
Assert.True(result);
Assert.NotNull(swDoc);
  • Assert.True(result);:
    • We are checking value of result variable is true.
    • If return value is true then our asseertion is true.
  • Assert.NotNull(swDoc);:
    • We are checking value of swDoc variable is notnull.
    • If value of swDoc variable is not null then our asseertion is true.

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 [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!!!

Updated: