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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 172k
  • Answers 172k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In order to have this code re-attached after the ajax… May 12, 2026 at 2:32 pm
  • Editorial Team
    Editorial Team added an answer If you want the actual selected item itself: $("#ID option:selected"); May 12, 2026 at 2:32 pm
  • Editorial Team
    Editorial Team added an answer What you're asking is a pretty complex question. Sure, at… May 12, 2026 at 2:32 pm

Related Questions

When my team first started out with SVN we all just used our first
My team has recently started using Lance Hunt's C# Coding Standards document as a
We have just started out with Team Foundation Server 2008 / Visual Studio Team
I am the CTO and sole developer for my company. I'm getting ready to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.