Using the Visual Studio Unit Testing Framework, I'm looking at two options:
Assert.AreEqual(myObject.GetType(), typeof(MyObject));
and
Assert.IsInstanceOfType(myObject, typeof(MyObject));
Is there a difference between these two options? Is one more "correct" than the other?
What is the standard way of doing this?
Q 10 - Which of the following is correct about a Unit Test Case? A - A Unit Test Case is a part of code which ensures that the another part of code (method) works as expected.
In the overall view of any unit test application, we will see that a unit test is a three-step processes. The first one is setup test or arrange test, Act test or execute unit test and Assert means verify test result with expected results.
The first example will fail if the types are not exactly the same while the second will only fail if myObject
is not assignable to the given type e.g.
public class MySubObject : MyObject { ... } var obj = new MySubObject(); Assert.AreEqual(obj.GetType(), typeof(MyObject)); //fails Assert.IsInstanceOfType(obj, typeof(MyObject)); //passes
Minor syntactical point: while the above Assert.AreEqual()
statements will work, the order of the parameters should be reversed, i.e., Assert.AreEqual(Type expected, Type actual)
.
So, in this case: Assert.AreEqual(typeof(MyObject), obj.GetType());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With