Assert.AreEqual - with Examples
Assert.AreEqual method verifies that specified values are equal. In simple words, Assert.AreEqual checks for the equality of the supplied arguments. Below are few examples in order to get a whiff of Assert.AreEqual.
Example 1 of Assert.AreEqual
In this first example we are simply verifying whether the two values (value in variable x and "Google Search") are equal or not.
The above test will pass. Lets see what happens if the above test fails. So in order to fail the above test we will make a very small change i.e. we will remove 'G' from 'Google Search'. So our new test looks like:
Now when you run the above test following error is shown:
Example 2 of Assert.AreEqual
This example is similar to above except that a user defined message "Dummy_message" (in our case) is displayed if the assertion fails.
The error message shown after running the above code is:
Example 3 of Assert.AreEqual
In this example the last parameter is boolean true. A boolean value indicates a case-sensitive or insensitive comparison. True indicates a case-insensitive comparison.
The above test will pass.
Example 4 of Assert.AreEqual
In this example we are verifying whether the two objects are equal. The assertion fails if the objects are not equal. In below example we are comparing two same objects. Next example compares two different objects.
The above test will pass. We have not considered obj1 in the Assert statement above.
Example 5 of Assert.AreEqual
Its similar to above but here a message is displayed if the assertion fails.
Result on running the above test will be:
Example 6 of Assert.AreEqual
Its similar to above except that it applies the specified formatting to the message displayed.
For more on Assert.AreEqual please have a look at MSDN.