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 3998056
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:28:41+00:00 2026-05-20T07:28:41+00:00

I am trying out the MVC3 Razor view engine, and one the features that

  • 0

I am trying out the MVC3 Razor view engine, and one the features that I am exploring is the ability to unit test views.

I have seen a number of examples where Razor views can be compiled into an assembly and rendered into a string. The problem is that it renders as a string, so I am left with doing string searches!

Here is what I am trying to do.

  1. Create a Razor helper
  2. Compile helper
  3. Run compiled helper, passing in a ViewModel
  4. Get the output of the helper as some sort of HTML/XML/tree structure

The reason that I want to do this is so that I can test for specific parts of the output. The helper will likely spit out HTML that includes various output gunk. What I want to do is to see if there is a Checkbox with a particular value (for example). If you have seen Selenium tests, it is similar to what I would like to do, except not as server driven tests.

Is there some way to get compiled Razor (or other view engine) views to emit something other than strings?

  • 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-05-20T07:28:41+00:00Added an answer on May 20, 2026 at 7:28 am

    The short answer is no, because view engines’ purpose in life is to spit out strings. Parsing those strings into an XML document is a way to give them a little structure, like @Craig-M suggested. But what you have to ask yourself is what you’re really testing. If your view compiles and generates some kind of HTML, there can be three problems with what it generated:

    • the data it displays is not correct. You would test for this at the controller level, so you can ignore it during view testing.
    • the MVC framework had an error and it generated the wrong HTML string. You don’t worry about this, because MVC has its own test suite and thanks to separation of concerns, it’s not your problem.
    • the HTML has broken the user interface.

    That last one would be very nice to test for, but why not test for it in javascript unit tests? Check out popular javascript unit test suites like JsUnit, FireUnit, QUnit, etc. They would all do a much better job than you could do of parsing out Razor output.

    I think the value assigned to a checkbox would probably be tested for in Controller testing. But to use your example, testing your checkbox’s value could be $(‘#theCheck’).val() == ‘the value’. And you can run these against a website running with dependency injected repositories or services to control things like ‘the value’.

    Just a thought.

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

Sidebar

Related Questions

Using MVC3, C#, and the Razor View Engine: I have a form that has
I am trying out the new razor view engine from MVC 3. The issue
I have a need to learn MVC 3 with the Razor view engine and
I'm using the Razor view engine with MVC 3 and I'm trying to make
I'm trying out ASP.NET MVC routing and have of course stumbled across a problem.
I just started trying out the mvc-mini-profiler in my MVC3 project with EF4.1 and
Context: Asp.Net MVC3 w/Razor I am trying to put a login form on a
Firstly, I'm new to MVC - and trying out ASP.NET MVC3. I want to
I am becoming more familiar with MVC 3 and the RAZOR view engine. I
I'm trying out MVC Scaffolding in a VB.NET MVC3 project and running into an

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.