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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:02:40+00:00 2026-06-16T16:02:40+00:00

I have an extension method for a helper class in an MVC4 project: public

  • 0

I have an extension method for a helper class in an MVC4 project:

public static class ExtensionMethods
{

    public static object Value<TModel, TProperty>(this Expression<Func<TModel, TProperty>> expression, ViewDataDictionary<TModel> viewData)
    {
        return ModelMetadata.FromLambdaExpression(expression, viewData).Model;
    }

}

This does something very simple in a manner that is anything but simple.

So, how can this best be unit tested? It would be preferable to avoid mocking static methods or using dependency injection, but I am open minded if these really are the only viable approaches in this case.

Is this just a flawed design that could be improved so as to be more amenable to unit testing?

  • 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-16T16:02:41+00:00Added an answer on June 16, 2026 at 4:02 pm

    This is indeed a flawed design and is presumably common. The correct method signature requires the parameter order to be switched:

    public static object Value<TModel, TProperty>(this ViewDataDictionary<TModel> viewData, Expression<Func<TModel, TProperty>> expression)
    {
        return ModelMetadata.FromLambdaExpression(expression, viewData).Model;
    }
    

    This can then be called from unit tests:

    Expression<Func<Model, string>> expression = (t => t.PropertyName);
    ExtensionMethods.Value<Model, string>(viewData, expression));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this extension method: public static string GetValueFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>>
I have the following method: public static string UlList(this HtmlHelper helper, List<IEntity> entities, string
I have a method which i want to convert to Extension Method public static
This extension method does not work on two separate development machines: public static string
The CollectionViewSource.GetDefaultView() method is not in Silverlight 3. In WPF I have this extension
I have an extension method for System.Object to serialize and deserialize objects using Json.Net
I have written an extension method in csharp for an MVCContrib Html helper and
I came across the following issue: I have this model public class Region {
I have written some extension methods that extend the html helper class for client
Is there a better way to do this? I have an HTML helper extension

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.