This is the Fundamentals of Unit Testing article series. In this article, we are discussing unit testing in the Visual Studio environment using a built-in Visual Studio unit testing project. In our previous article, we saw what unit testing is and how to do unit testing in the Visual Studio environment, you can read them here.
In our previous article, we used the AreEqual() method with the Assert class to determine whether or not two results are equal.
In today's article, we will understand the difference between the AreEqual() and AreEqual<T>() methods. Then we will discuss various overloaded versions of the AreEqual<T>() method. So, let's try a small example.
A point to be made is that both AreEqual() and AreEqual<T> are static methods, so we can invoke them using the class name (Assert) to which they belong.
AreEqual() method of asserting class
From Visual Studio, we see that this function takes two objects and returns void. If we observe closely we can see that we will compare a string value with an integer value, though both values are equal they are different in terms of Data type.
And we are seeing that there is no syntactical issue in this code. But we will see that, it will throw an exception at run time. So, let's run the unit test example.
We are seeing that it's throwing a type exception at run time. The reason for this error is that AreEqual() is not a strongly type-checking function. So depending on the parameter type, the .NET environment tries to make it a strong type implicitly and when it fails, it throws an exception.
Understand AreEqual<T>() method
It's a parameterized function and takes a parameter of T type, in other words, any data type. Here is a small implementation of the previous example with the AreEqual<T>() method. We see that AreEqual<T> has three overloaded flavors and the first overloaded method takes T as a type argument and a to parameter of the same type to be compared.
Now, if we forcefully try to compare the integer and string then we will see that.
At compile time, the compiler is complaining because when we are passing int instead of “T”, it's expecting both integer arguments, but in reality, we are passing one string and one integer argument. So that the error.
Fine, we will now have a look at various overloaded functions of the AreEqual<T>() method.
First overloaded method
It takes two arguments, the arguments are the value to compare and another is the value with whom to compare.
Second overloaded method
The second overloaded method takes three arguments, the first two arguments are the value to compare with which to compare and the third argument is the message string, if it fails then the message will be displayed.
Third overloaded method
Here is the third overloaded method that will take 4 parameters, the first two parameters are two objects the third parameter is the message that it will display in fail and the fourth parameter is an array of objects that we can specify to format the message.
Conclusion
I hope you have understood the difference between the AreEqual() and AreEqual<T>() methods. In a future article, we will understand a few more functions of the Assert class.