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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:16:52+00:00 2026-05-28T14:16:52+00:00

I use templated helpers in an ASP.NET MVC 3 project. One display template had

  • 0

I use templated helpers in an ASP.NET MVC 3 project. One display template had a typo — an accidental extra code block — that caused a compiler error when that view was returned (“Don’t put @if in a code block,” you know).

All fine, except that the test method that called that view was still succeeding. I’m having a hard time figuring out how I would fail this code block in a unit test.

Here’s the bad display template:

@model MemberSelectorViewModel
@{ 
    Layout = "~/Views/Shared/_DisplayFormItem.cshtml";
}
@section DataContent {
  @{  // <- this was the typo, and it... 
    @if (Model.idMember.HasValue)   // <- causes this to throw a compiler error
    { 
        @Html.ActionLink(Model.FullName, "Details", "Member", new { id = Model.idMember.Value }, null )
        @Html.HiddenFor(m=> m.idMember)
    }
  }
}

Here’s the test that I think ought to be failing:

[TestMethod]
public void DetailsReturnsView()
{
    MemberJobController target = new MemberJobController(TestHarness.Context);
    memberjob mj = TestHarness.UnitOfWork.MemberJobRepository.FirstOrDefault(x => true);

    ActionResult result = target.Details(mj.idmemberjob);  // <- this should hit the compiler error, I would have thought 

    Assert.IsNotNull(result);

}

But that test succeeds.

Any idea how I’d write a test that would fail on the ” @{ @if () ” typo in a templated helper?

  • 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-28T14:16:53+00:00Added an answer on May 28, 2026 at 2:16 pm

    I think you might have misunderstood something about how unit tests work on a controller. They never hit the view actually. So no matter how many errors you did in your view, when you call the controller action in a unit test, all that happens is that the body of this action is executed. And that’s all. It will never go to the view.

    So if you want to test your views you are no longer doing unit tests. You are doing integration tests where you send an HTTP request to your site which is deployed on a staging server and verifying that the returned HTML complies to your requirements. In this case if you made a typo or something in your view the actual HTML which is returned when you hit the particular controller action with an HTTP request will obviously differ than what you expect.

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

Sidebar

Related Questions

I'm starting to use DataAnnotations in ASP.NET MVC and strongly typed template helpers. Now
I'm wondering how one could use template databinding to accomplish what the following code
I'm trying to create a templated control with Asp.Net MVC. By templated control, I
One of the new features in ASP.NET MVC 2 Preview 1 is support for
With ASP.net MVC I have a Model like so: public class Project { public
I use the following code to render a template to a string that I
I am using the latest version Telerik MVC controls . I am using ASP.NET
So, the title should speak for itself. To create re-usable components in ASP.NET MVC,
I'm trying to build a custom control in ASP.NET MVC. The thing that I
I am creating an asp.net mvc 2 application generating my view model dynamically depending

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.