SOLIDWORKS C# API - Test ApplyUnitConversion Method

18 minute read

Objective

I want to:

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

Demo Video

Watch the video below to learn how to Test ApplyUnitConversion 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 ApplyUnitConversion method we going to use in our test class make as public.

public (double, double, double) ApplyUnitConversion(IPointViewModel inputPoint, double lengthConversionFactor)
{
  double X, Y, Z;

  X = inputPoint.XPoint * lengthConversionFactor;
  Y = inputPoint.YPoint * lengthConversionFactor;
  Z = inputPoint.ZPoint * lengthConversionFactor;

  return (X, Y, Z);
}

Above changes we need to do in our MainWindowViewModel class.


Add Test Cases

To test ApplyUnitConversion method we need to add below ๐Ÿ‘‡๐Ÿป test cases.

  • Test 1 - ApplyUnitConversion_PositiveValues_ReturnsConvertedCoordinates
  • Test 2 - ApplyUnitConversion_ZeroConversionFactor_ReturnsZeroCoordinates
  • Test 3 - ApplyUnitConversion_NegativeConversionFactor_ReturnsNegativeScaledCoordinates
  • Test 4 - ApplyUnitConversion_ZeroPointValues_ReturnsZeroCoordinates
  • Test 5 - ApplyUnitConversion_MixedValues_ReturnsCorrectlyScaledCoordinates

Add [Test 1]

In this section, we set up ApplyUnitConversion_PositiveValues_ReturnsConvertedCoordinates.

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

[Fact]
public void ApplyUnitConversion_PositiveValues_ReturnsConvertedCoordinates()
{
  // Arrange
  var mockPoint = new Mock<IPointViewModel>();
  mockPoint.Setup(p => p.XPoint).Returns(2);
  mockPoint.Setup(p => p.YPoint).Returns(3);
  mockPoint.Setup(p => p.ZPoint).Returns(4);
  double lengthConversionFactor = 2.5;

  // Act
  var result = _viewModel.ApplyUnitConversion(mockPoint.Object, lengthConversionFactor);

  // Assert
  Assert.Equal((5.0, 7.5, 10.0), result);
}

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

test-method-1

Explanation of above ApplyUnitConversion_PositiveValues_ReturnsConvertedCoordinates is give below.

[Fact]
public void ApplyUnitConversion_PositiveValues_ReturnsConvertedCoordinates()
{

}
  • [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 ApplyUnitConversion_PositiveValues_ReturnsConvertedCoordinates 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 ApplyUnitConversion_PositiveValues_ReturnsConvertedCoordinates method.
    • Because of this requirment we need to give public accessor.
  • void :
    • This is the return type of ApplyUnitConversion_PositiveValues_ReturnsConvertedCoordinates method.
    • Generally we donโ€™t return anything from test method.
    • Because of this we return void means we are not returning anything.
  • ApplyUnitConversion_PositiveValues_ReturnsConvertedCoordinates
    • 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.

First we arrange our test method and setup variables and methods before testing.

var mockPoint = new Mock<IPointViewModel>();
  • var: This is a keyword in C# that means โ€œvariableโ€. Itโ€™s used to declare a new variable.

  • mockPoint: This is name of object we want to create.

  • new: This is a keyword that creates a new instance of a class.

  • Mock<IPointViewModel>():

    • Mock is the class that creates a โ€œmockโ€ object, which is a fake version of a real object.
    • In our case, itโ€™s a mock version of the IPointViewModel object.
mockPoint.Setup(p => p.XPoint).Returns(2);
  • mockPoint is the variable we created previously.
  • Setup is a method that sets up the mock object to behave in a certain way.
  • p => p.XPoint is a lambda expression that says โ€œwhen someone asks for the XPoint property of this mock object, do somethingโ€.
  • Returns(2) says โ€œwhen someone asks for the XPoint property, return the value 2โ€. This means that if someone tries to get the X-coordinate of this fake point, it will always be 2.
mockPoint.Setup(p => p.YPoint).Returns(3);
  • Above line is similar to the previous one, but it sets up the YPoint property to return the value 3.
  • This means that if someone tries to get the Y-coordinate of this fake point, it will always be 3.
mockPoint.Setup(p => p.ZPoint).Returns(4);
  • Above line is similar to the previous one, but it sets up the ZPoint property to return the value 4.
  • This means that if someone tries to get the Z-coordinate of this fake point, it will always be 4.
double lengthConversionFactor = 2.5;
  • double is a data type that means โ€œa decimal numberโ€.
  • lengthConversionFactor is the name of the variable.
  • 2.5 is the value assigned to the variable.
  • This line creates a new variable called lengthConversionFactor and sets its value to 2.5.

Now we finished with our arrangement part, we will now continue with Act part of our test method.

// Act
var result = _viewModel.ApplyUnitConversion(mockPoint.Object, lengthConversionFactor);
  • var is a keyword in C# that means โ€œvariableโ€. Itโ€™s used to declare a new variable.
  • result is the name of the variable.
  • _viewModel is an object that represents the MainWindowViewModel view model.
  • ApplyUnitConversion is a method of the MainWindowViewModel view model that applies a unit conversion to a point.
  • mockPoint.Object is the mock point object that we created earlier, which has X, Y, and Z coordinates set to 2, 3, and 4 respectively.
  • lengthConversionFactor is the conversion factor that we defined earlier, which is 2.5.
  • This line of code is calling the ApplyUnitConversion method on the MainWindowViewModel view model, passing in the mock point and the conversion factor, and storing the result in the result variable.

With this we finished with Act part of our test method. Now we will continue with Assert part.

// Assert
Assert.Equal((5.0, 7.5, 10.0), result);
  • Assert is a keyword in xUnit testing framework and is used to verify that a certain condition is true.
  • Equal is a method of the Assert class that checks if two values are equal.
  • (5.0, 7.5, 10.0) is a tuple that represents the expected result. In this case, itโ€™s a 3D point with X, Y, and Z coordinates.
  • result is the variable that holds the actual result of the ApplyUnitConversion method, which we explained earlier.
  • Above line of code is checking if the actual result of the ApplyUnitConversion method is equal to the expected result, which is the point (5.0, 7.5, 10.0).

Add [Test 2]

In this section, we set up ApplyUnitConversion_ZeroConversionFactor_ReturnsZeroCoordinates.

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

[Fact]
public void ApplyUnitConversion_ZeroConversionFactor_ReturnsZeroCoordinates()
{
  // Arrange
  var mockPoint = new Mock<IPointViewModel>();
  mockPoint.Setup(p => p.XPoint).Returns(2);
  mockPoint.Setup(p => p.YPoint).Returns(3);
  mockPoint.Setup(p => p.ZPoint).Returns(4);
  double lengthConversionFactor = 0;

  // Act
  var result = _viewModel.ApplyUnitConversion(mockPoint.Object, lengthConversionFactor);

  // Assert
  Assert.Equal((0.0, 0.0, 0.0), result);
}

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

test-method-2

Explanation of above ApplyUnitConversion_ZeroConversionFactor_ReturnsZeroCoordinates is give below.

[Fact]
public void ApplyUnitConversion_ZeroConversionFactor_ReturnsZeroCoordinates()
{

}
  • [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 ApplyUnitConversion_ZeroConversionFactor_ReturnsZeroCoordinates 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 ApplyUnitConversion_ZeroConversionFactor_ReturnsZeroCoordinates method.
    • Because of this requirment we need to give public accessor.
  • void :
    • This is the return type of ApplyUnitConversion_ZeroConversionFactor_ReturnsZeroCoordinates method.
    • Generally we donโ€™t return anything from test method.
    • Because of this we return void means we are not returning anything.
  • ApplyUnitConversion_ZeroConversionFactor_ReturnsZeroCoordinates
    • 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.

First we arrange our test method and setup variables and methods before testing.

var mockPoint = new Mock<IPointViewModel>();
mockPoint.Setup(p => p.XPoint).Returns(2);
mockPoint.Setup(p => p.YPoint).Returns(3);
mockPoint.Setup(p => p.ZPoint).Returns(4);
  • The explanation for the above code is in the previous section. Thus not explaining here.
double lengthConversionFactor = 0;
  • double is a data type that means โ€œa decimal numberโ€.
  • lengthConversionFactor is the name of the variable.
  • 0 is the value assigned to the variable.
  • This line creates a new variable called lengthConversionFactor and sets its value to 0.

Now we finished with our arrangement part, we will now continue with Act part of our test method.

// Act
var result = _viewModel.ApplyUnitConversion(mockPoint.Object, lengthConversionFactor);
  • var is a keyword in C# that means โ€œvariableโ€. Itโ€™s used to declare a new variable.
  • result is the name of the variable.
  • _viewModel is an object that represents the MainWindowViewModel view model.
  • ApplyUnitConversion is a method of the MainWindowViewModel view model that applies a unit conversion to a point.
  • mockPoint.Object is the mock point object that we created earlier, which has X, Y, and Z coordinates set to 2, 3, and 4 respectively.
  • lengthConversionFactor is the conversion factor that we defined earlier, which is 0.
  • This line of code is calling the ApplyUnitConversion method on the MainWindowViewModel view model, passing in the mock point and the conversion factor, and storing the result in the result variable.

With this we finished with Act part of our test method. Now we will continue with Assert part.

// Assert
Assert.Equal((0.0, 0.0, 0.0), result);
  • Assert is a keyword in xUnit testing framework and is used to verify that a certain condition is true.
  • Equal is a method of the Assert class that checks if two values are equal.
  • (0.0, 0.0, 0.0) is a tuple that represents the expected result. In this case, itโ€™s a 3D point with X, Y, and Z coordinates.
  • result is the variable that holds the actual result of the ApplyUnitConversion method, which we explained earlier.
  • Above line of code is checking if the actual result of the ApplyUnitConversion method is equal to the expected result, which is the point (0.0, 0.0, 0.0).

Add [Test 3]

In this section, we set up ApplyUnitConversion_NegativeConversionFactor_ReturnsNegativeScaledCoordinates.

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

[Fact]
public void ApplyUnitConversion_NegativeConversionFactor_ReturnsNegativeScaledCoordinates()
{
  // Arrange
  var mockPoint = new Mock<IPointViewModel>();
  mockPoint.Setup(p => p.XPoint).Returns(2);
  mockPoint.Setup(p => p.YPoint).Returns(3);
  mockPoint.Setup(p => p.ZPoint).Returns(4);
  double lengthConversionFactor = -1.5;

  // Act
  var result = _viewModel.ApplyUnitConversion(mockPoint.Object, lengthConversionFactor);

  // Assert
  Assert.Equal((-3.0, -4.5, -6.0), result);
}

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

test-method-3

Explanation of above ApplyUnitConversion_NegativeConversionFactor_ReturnsNegativeScaledCoordinates is give below.

[Fact]
public void ApplyUnitConversion_NegativeConversionFactor_ReturnsNegativeScaledCoordinates()
{

}
  • [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 ApplyUnitConversion_NegativeConversionFactor_ReturnsNegativeScaledCoordinates 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 ApplyUnitConversion_NegativeConversionFactor_ReturnsNegativeScaledCoordinates method.
    • Because of this requirment we need to give public accessor.
  • void :
    • This is the return type of ApplyUnitConversion_NegativeConversionFactor_ReturnsNegativeScaledCoordinates method.
    • Generally we donโ€™t return anything from test method.
    • Because of this we return void means we are not returning anything.
  • ApplyUnitConversion_NegativeConversionFactor_ReturnsNegativeScaledCoordinates
    • 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.

First we arrange our test method and setup variables and methods before testing.

var mockPoint = new Mock<IPointViewModel>();
mockPoint.Setup(p => p.XPoint).Returns(2);
mockPoint.Setup(p => p.YPoint).Returns(3);
mockPoint.Setup(p => p.ZPoint).Returns(4);
  • The explanation for the above code is in the previous section. Thus not explaining here.
double lengthConversionFactor = -1.5;
  • double is a data type that means โ€œa decimal numberโ€.
  • lengthConversionFactor is the name of the variable.
  • -1.5 is the value assigned to the variable.
  • This line creates a new variable called lengthConversionFactor and sets its value to -1.5.

Now we finished with our arrangement part, we will now continue with Act part of our test method.

// Act
var result = _viewModel.ApplyUnitConversion(mockPoint.Object, lengthConversionFactor);
  • var is a keyword in C# that means โ€œvariableโ€. Itโ€™s used to declare a new variable.
  • result is the name of the variable.
  • _viewModel is an object that represents the MainWindowViewModel view model.
  • ApplyUnitConversion is a method of the MainWindowViewModel view model that applies a unit conversion to a point.
  • mockPoint.Object is the mock point object that we created earlier, which has X, Y, and Z coordinates set to 2, 3, and 4 respectively.
  • lengthConversionFactor is the conversion factor that we defined earlier, which is 0.
  • This line of code is calling the ApplyUnitConversion method on the MainWindowViewModel view model, passing in the mock point and the conversion factor, and storing the result in the result variable.

With this we finished with Act part of our test method. Now we will continue with Assert part.

// Assert
Assert.Equal((-3.0, -4.5, -6.0), result);
  • Assert is a keyword in xUnit testing framework and is used to verify that a certain condition is true.
  • Equal is a method of the Assert class that checks if two values are equal.
  • (-3.0, -4.5, -6.0) is a tuple that represents the expected result. In this case, itโ€™s a 3D point with X, Y, and Z coordinates.
  • result is the variable that holds the actual result of the ApplyUnitConversion method, which we explained earlier.
  • Above line of code is checking if the actual result of the ApplyUnitConversion method is equal to the expected result, which is the point (-3.0, -4.5, -6.0).

Add [Test 4]

In this section, we set up ApplyUnitConversion_ZeroPointValues_ReturnsZeroCoordinates.

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

[Fact]
public void ApplyUnitConversion_ZeroPointValues_ReturnsZeroCoordinates()
{
  // Arrange
  var mockPoint = new Mock<IPointViewModel>();
  mockPoint.Setup(p => p.XPoint).Returns(0);
  mockPoint.Setup(p => p.YPoint).Returns(0);
  mockPoint.Setup(p => p.ZPoint).Returns(0);
  double lengthConversionFactor = 2.5;

  // Act
  var result = _viewModel.ApplyUnitConversion(mockPoint.Object, lengthConversionFactor);

  // Assert
  Assert.Equal((0.0, 0.0, 0.0), result);
}

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

test-method-4

Explanation of above ApplyUnitConversion_ZeroPointValues_ReturnsZeroCoordinates is give below.

[Fact]
public void ApplyUnitConversion_ZeroPointValues_ReturnsZeroCoordinates()
{

}
  • [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 ApplyUnitConversion_ZeroPointValues_ReturnsZeroCoordinates 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 ApplyUnitConversion_ZeroPointValues_ReturnsZeroCoordinates method.
    • Because of this requirment we need to give public accessor.
  • void :
    • This is the return type of ApplyUnitConversion_ZeroPointValues_ReturnsZeroCoordinates method.
    • Generally we donโ€™t return anything from test method.
    • Because of this we return void means we are not returning anything.
  • ApplyUnitConversion_ZeroPointValues_ReturnsZeroCoordinates
    • 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.

First we arrange our test method and setup variables and methods before testing.

var mockPoint = new Mock<IPointViewModel>();
  • var: This is a keyword in C# that means โ€œvariableโ€. Itโ€™s used to declare a new variable.

  • mockPoint: This is name of object we want to create.

  • new: This is a keyword that creates a new instance of a class.

  • Mock<IPointViewModel>():

    • Mock is the class that creates a โ€œmockโ€ object, which is a fake version of a real object.
    • In our case, itโ€™s a mock version of the IPointViewModel object.
mockPoint.Setup(p => p.XPoint).Returns(0);
  • mockPoint is the variable we created previously.
  • Setup is a method that sets up the mock object to behave in a certain way.
  • p => p.XPoint is a lambda expression that says โ€œwhen someone asks for the XPoint property of this mock object, do somethingโ€.
  • Returns(0) says โ€œwhen someone asks for the XPoint property, return the value 0โ€. This means that if someone tries to get the X-coordinate of this fake point, it will always be 0.
mockPoint.Setup(p => p.YPoint).Returns(0);
  • Above line is similar to the previous one, but it sets up the YPoint property to return the value 0.
  • This means that if someone tries to get the Y-coordinate of this fake point, it will always be 0.
mockPoint.Setup(p => p.ZPoint).Returns(0);
  • Above line is similar to the previous one, but it sets up the ZPoint property to return the value 0.
  • This means that if someone tries to get the Z-coordinate of this fake point, it will always be 0.
double lengthConversionFactor = 2.5;
  • double is a data type that means โ€œa decimal numberโ€.
  • lengthConversionFactor is the name of the variable.
  • 2.5 is the value assigned to the variable.
  • This line creates a new variable called lengthConversionFactor and sets its value to 2.5.

Now we finished with our arrangement part, we will now continue with Act part of our test method.

// Act
var result = _viewModel.ApplyUnitConversion(mockPoint.Object, lengthConversionFactor);
  • var is a keyword in C# that means โ€œvariableโ€. Itโ€™s used to declare a new variable.
  • result is the name of the variable.
  • _viewModel is an object that represents the MainWindowViewModel view model.
  • ApplyUnitConversion is a method of the MainWindowViewModel view model that applies a unit conversion to a point.
  • mockPoint.Object is the mock point object that we created earlier, which has X, Y, and Z coordinates set to 0.
  • lengthConversionFactor is the conversion factor that we defined earlier, which is 2.5.
  • This line of code is calling the ApplyUnitConversion method on the MainWindowViewModel view model, passing in the mock point and the conversion factor, and storing the result in the result variable.

With this we finished with Act part of our test method. Now we will continue with Assert part.

// Assert
Assert.Equal((0.0, 0.0, 0.0), result);
  • Assert is a keyword in xUnit testing framework and is used to verify that a certain condition is true.
  • Equal is a method of the Assert class that checks if two values are equal.
  • (0.0, 0.0, 0.0) is a tuple that represents the expected result. In this case, itโ€™s a 3D point with X, Y, and Z coordinates.
  • result is the variable that holds the actual result of the ApplyUnitConversion method, which we explained earlier.
  • Above line of code is checking if the actual result of the ApplyUnitConversion method is equal to the expected result, which is the point (0.0, 0.0, 0.0).

Add [Test 5]

In this section, we set up ApplyUnitConversion_MixedValues_ReturnsCorrectlyScaledCoordinates.

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

[Fact]
public void ApplyUnitConversion_MixedValues_ReturnsCorrectlyScaledCoordinates()
{
  // Arrange
  var mockPoint = new Mock<IPointViewModel>();
  mockPoint.Setup(p => p.XPoint).Returns(-1);
  mockPoint.Setup(p => p.YPoint).Returns(0);
  mockPoint.Setup(p => p.ZPoint).Returns(5);
  double lengthConversionFactor = 3;

  // Act
  var result = _viewModel.ApplyUnitConversion(mockPoint.Object, lengthConversionFactor);

  // Assert
  Assert.Equal((-3.0, 0.0, 15.0), result);
}

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

test-method-5

Explanation of above ApplyUnitConversion_MixedValues_ReturnsCorrectlyScaledCoordinates is give below.

[Fact]
public void ApplyUnitConversion_MixedValues_ReturnsCorrectlyScaledCoordinates()
{

}
  • [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 ApplyUnitConversion_MixedValues_ReturnsCorrectlyScaledCoordinates 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 ApplyUnitConversion_MixedValues_ReturnsCorrectlyScaledCoordinates method.
    • Because of this requirment we need to give public accessor.
  • void :
    • This is the return type of ApplyUnitConversion_MixedValues_ReturnsCorrectlyScaledCoordinates method.
    • Generally we donโ€™t return anything from test method.
    • Because of this we return void means we are not returning anything.
  • ApplyUnitConversion_MixedValues_ReturnsCorrectlyScaledCoordinates
    • 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.

First we arrange our test method and setup variables and methods before testing.

var mockPoint = new Mock<IPointViewModel>();
  • var: This is a keyword in C# that means โ€œvariableโ€. Itโ€™s used to declare a new variable.

  • mockPoint: This is name of object we want to create.

  • new: This is a keyword that creates a new instance of a class.

  • Mock<IPointViewModel>():

    • Mock is the class that creates a โ€œmockโ€ object, which is a fake version of a real object.
    • In our case, itโ€™s a mock version of the IPointViewModel object.
mockPoint.Setup(p => p.XPoint).Returns(-1);
  • mockPoint is the variable we created previously.
  • Setup is a method that sets up the mock object to behave in a certain way.
  • p => p.XPoint is a lambda expression that says โ€œwhen someone asks for the XPoint property of this mock object, do somethingโ€.
  • Returns(-1) says โ€œwhen someone asks for the XPoint property, return the value -1โ€. This means that if someone tries to get the X-coordinate of this fake point, it will always be -1.
mockPoint.Setup(p => p.YPoint).Returns(0);
  • Above line is similar to the previous one, but it sets up the YPoint property to return the value 0.
  • This means that if someone tries to get the Y-coordinate of this fake point, it will always be 0.
mockPoint.Setup(p => p.ZPoint).Returns(5);
  • Above line is similar to the previous one, but it sets up the ZPoint property to return the value 5.
  • This means that if someone tries to get the Z-coordinate of this fake point, it will always be 5.
double lengthConversionFactor = 3;
  • double is a data type that means โ€œa decimal numberโ€.
  • lengthConversionFactor is the name of the variable.
  • 3 is the value assigned to the variable.
  • This line creates a new variable called lengthConversionFactor and sets its value to 3.

Now we finished with our arrangement part, we will now continue with Act part of our test method.

// Act
var result = _viewModel.ApplyUnitConversion(mockPoint.Object, lengthConversionFactor);
  • var is a keyword in C# that means โ€œvariableโ€. Itโ€™s used to declare a new variable.
  • result is the name of the variable.
  • _viewModel is an object that represents the MainWindowViewModel view model.
  • ApplyUnitConversion is a method of the MainWindowViewModel view model that applies a unit conversion to a point.
  • mockPoint.Object is the mock point object that we created earlier, which has X, Y, and Z coordinates set to -1, 0, 5.
  • lengthConversionFactor is the conversion factor that we defined earlier, which is 3.
  • This line of code is calling the ApplyUnitConversion method on the MainWindowViewModel view model, passing in the mock point and the conversion factor, and storing the result in the result variable.

With this we finished with Act part of our test method. Now we will continue with Assert part.

// Assert
Assert.Equal((-3.0, 0.0, 15.0), result);
  • Assert is a keyword in xUnit testing framework and is used to verify that a certain condition is true.
  • Equal is a method of the Assert class that checks if two values are equal.
  • (-3.0, 0.0, 15.0) is a tuple that represents the expected result. In this case, itโ€™s a 3D point with X, Y, and Z coordinates.
  • result is the variable that holds the actual result of the ApplyUnitConversion method, which we explained earlier.
  • Above line of code is checking if the actual result of the ApplyUnitConversion method is equal to the expected result, which is the point (-3.0, 0.0, 15.0).

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 ApplyUnitConversion 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: