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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:57:37+00:00 2026-05-12T07:57:37+00:00

Our team has just started unittesting and mocking, and we have run into some

  • 0

Our team has just started unittesting and mocking, and we have run into some discussion considering extension methods. The question is what is a good approach to testing classes, that make use of extension methods. I.e. we have an Enum like this..

public enum State
{
    [LangID(2817)]
    Draft = 0,
    [LangID(2832)]
    Booked = 1,
    [LangID(1957)]
    Overdue = 2,
    [LangID(2834)]
    Checked = 3,
}

Which makes use of the extension method:

public static string GetDescription(this Enum _enum)
{
    Type type = _enum.GetType();
    MemberInfo[] memInfo = type.GetMember(_enum.ToString()); 
    if (memInfo != null && memInfo.Length > 0)
    {
        object[] attrs = memInfo[0].GetCustomAttributes(typeof(LangID), false);
        if (attrs != null && attrs.Length > 0)
            return LanguageDB.GetString(((LangID)attrs[0]).ID);
    }
    return _enum.ToString();
}

Which again will be called by the class under test, like so ..

public class SUT(){

  public void MethodUnderTest(){
      string description = SomeObject.Status.GetDescription();//Status is Type State
  }
}

In this example the enum is getting a description in the language of the user, via LanguageDB, which unfortunately is not injected inside the class since it’s static. We could naturally refractor the lot, but this would be a big investment, considering the code is working almost flawless.
Any good suggestion?

  • 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-12T07:57:37+00:00Added an answer on May 12, 2026 at 7:57 am

    If you’re using MS’ test suite you can do a very simple refactoring and be able to use Accessors to inject a mock into your static type object.

    Let’s say you’ve got code like this:

    public static void Save(this Entity data)
    {
      Repository.Instance.Save(data);
    }
    

    A static within a static… hard to test? Not really. Modify the extension class thusly:

    private static Repository Instance
    {
      get
      {
        return _repository ?? Repository.Instance;
      }
    }
    private static Repository _repository = null;
    
    public static void Save(this Entity data)
    {
      Instance.Save(data);
    }
    

    Simple refactor. Now, you can use the accessor to set your mock at test time…

    [TestInitialize(), DebuggerStepThrough]
    public void Setup()
    {
      MyEntityExtensions_Accessor._repository = new Mock<IRepository>();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our team has just started developing for the Sitecore CMS. We find that after
Our team has recently been considering pushing out a minor registry fix to users
Our team has just moved from Netbeans to IntelliJ 9 Ultimate and need to
I've just implemented kanban on my team and have started tracking the states of
We have various teams building Silverlight applications within our organisation. We have just started
When my team first started out with SVN we all just used our first
My development team has run into a design issue. I'm hoping someone can help
Our team has recently been working on a logic and data layer for our
Our Team Foundation Server has to build three different versions of the same solution.
My capstone team has decided to use Qooxdoo as the front end for our

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.