Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8373149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:36:29+00:00 2026-06-09T14:36:29+00:00

I am pretty new to testing and have been writing my Controller tests as

  • 0

I am pretty new to testing and have been writing my Controller tests as follows:

var result = myController.Create(It.IsAny<int>());

Assert.IsInstanceOfType(result, typeof(ViewResult));
Assert.AreEqual(string.Empty, ((ViewResult)result).ViewName, "Default view expected.");
Assert.IsInstanceOfType(((ViewResult)result).ViewData.Model, typeof(FooBarCreateEditViewModel));

This works fine but is not as readable in that I am having to perform a cast for Tests 2 & 3. An alternative approach might be to do the following:

var result = (ViewResult)myController.Create(It.IsAny<int>());

Assert.AreEqual(string.Empty, result.ViewName, "Default view expected.");
Assert.IsInstanceOfType(result.ViewData.Model, typeof(FooBarCreateEditViewModel));

This also seems to work and is more readable in my opinion. My problem with it is that it relies on the “act” part of the test to perform the IsInstanceOfType test, rather than explicitly defining an Assert to do the job.

So is my second apporach ok to continue with? I am likely to run into issues with relying on the cast to throw an error should the method return a different type?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:36:31+00:00Added an answer on June 9, 2026 at 2:36 pm

    You could use MVCContrib.TestHelper. Allows you to write very readable unit tests like:

    // arrange
    ...
    
    // act
    var actual = controller.Create(123);
    
    // assert
    actual
        .AssertViewRendered()
        .WithViewData<FooBarCreateEditViewModel>();
    

    Checkout the following sample unit test I wrote.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Auto-generation of .NET unit tests I am pretty new to Unit Testing
I have started practicing TDD approach. I am pretty much new to unit testing.
I am pretty new to NUnit (and automated testing in general). I have recently
I am pretty new to XNA, and I have been trying to figure this
I'm pretty new to testing and mocking and i am trying to write a
Pretty new to MVC and the like. I have a class the looks like
I'm pretty new to Eclipse plugins and I want to create a VerticalRuler, I
I have been writing a program that watches a directory and when files are
I am 'relatively new' to unit-testing and TDD. Only more recently have I completed
I've been exploring the use of mock objects in unit testing and have been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.