Jonathan Tuzman
Jonathan Tuzman

Reputation: 13289

Assert.IsInstanceOfType won't accept Type as argument

I'm trying to test that some objects are of a certain type:

 [TestInitialize]
        public void SetUp()
        {
            exam = new Exam(examId, name, date, templateId);

            tabViewModel = new TabControlViewModel(exam);

            tabs = new List<SingleTabViewModel>(tabViewModel.Tabs);

            // Not using this in the current code, but including here to make
            // clear the expected type of the tabs
            examsTab = tabs[0] as ExamsTabViewModel;
            compareTab = tabs[1] as CompareExamsTabViewModel;
            templatesTab = tabs[2] as TemplatesTabViewModel;
        }

        [TestMethod]
        public void TestTabTypes()
        {
            Assert.IsInstanceOfType(tabs[0], ExamsTabViewModel);
            Assert.IsInstanceOfType(tabs[1], CompareExamsTabViewModel);
            Assert.IsInstanceOfType(tabs[2], TemplatesTabViewModel);
        }

But in the assertions, the types are giving the error:

'ExamsTabViewModel' is a type, which is not valid in the given context.

Even though the assertion's signature is

void Assert.IsInstanceOfType(object value, Type expectedType)

Why isn't this working?

Upvotes: 0

Views: 805

Answers (2)

Guru Stron
Guru Stron

Reputation: 142983

Try changing your assertions to Assert.IsInstanceOfType(tabs[0], typeof(ExamsTabViewModel)), cause you need to provide an instance of Type, not the type name.

Upvotes: 0

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239764

You want in instance of the Type class, not the type name. An easy way to get the former from the latter is typeof:

    [TestMethod]
    public void TestTabTypes()
    {
        Assert.IsInstanceOfType(tabs[0], typeof(ExamsTabViewModel));
        Assert.IsInstanceOfType(tabs[1], typeof(CompareExamsTabViewModel));
        Assert.IsInstanceOfType(tabs[2], typeof(TemplatesTabViewModel));
    }

Upvotes: 3

Related Questions