47

I'm kind of new to unit testing, using Microsoft.VisualStudio.TestTools.UnitTesting;

The 0.GetType() is actually System.RuntimeType, so what kind of test do I need to write to pass Assert.IsInstanceOfType(0.GetType(), typeof(int))?

--- following up, this is my own user error... Assert.IsInstanceOfType(0, typeof(int))

Anthony
  • 9,451
  • 9
  • 45
  • 72
David
  • 19,389
  • 12
  • 63
  • 87
  • What are you trying to accomplish? There's no purpose to assert that a constant is of a specific type. If this is toy code for the sake of an example, it's not specific enough. – Michael Meadows Mar 26 '09 at 16:41
  • 1
    This sample if for the sake of this question. In my actual test I'm doing some reflection and and getting a property which is of type int but the test fails... however Assert.IsTrue(0.GetType() == typeof(int)) will pass – David Mar 26 '09 at 16:44
  • I'd say that this question can be deleted, it is not helpful – David Mar 26 '09 at 16:50
  • 4
    While it may not be helpful to anyone now, it could be helpful in the future. I'm always thankful for finding hints/answers on couple year old blog posts that help solve a problem. – Samuel Mar 26 '09 at 17:03
  • 1
    I will say this question did solve my problem although it had nothing to do with the 0.gettype(), but being new to testing also the testing the syntax in there isn't the greatest. cheers. – Brian Oct 07 '09 at 20:44

2 Answers2

82

Change the call to the following

Assert.IsInstanceOfType(0, typeof(int));

The first parameter is the object being tested, not the type of the object being tested. by passing 0.GetType(), you were saying is "RunTimeType" an instance of System.int which is false. Under the covers thes call just resolves to

if (typeof(int).IsInstanceOfType(0))
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
19

Looks like it should be

Assert.IsInstanceOfType(0, typeof(int))

Your expression is currently evaluating to see if RunTimeType is an instance of RunTimeType, which it isn't.

smholloway
  • 589
  • 7
  • 14
Lee
  • 1,125
  • 6
  • 7