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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:25:13+00:00 2026-05-16T23:25:13+00:00

Extension methods are not good for testing (that’s described here: Mocking Extension Methods with

  • 0

Extension methods are not good for testing (that’s described here: Mocking Extension Methods with Moq, http://www.clariusconsulting.net/blogs/kzu/archive/2009/12/22/Howtomockextensionmethods.aspx).

But probably there are some solutions for mocking of Unity methods? In my case I have the following function:

public class MyManager
{
    public MyManager(IUnityContainer container) : base(container) { }

    public IResult DoJob(IData data)
    {
        IMyLog log = MyContainer.Resolve<IMyLog>();

        ... use log.Id ...

        MyContainer.Resolve<...>();//usage for other purposes...
    }

I want to be sure that ‘DoJob’ method will always get ‘IMyLog’ object from container, but not from other sources… how could I test that?

My original idea was to change ‘DoJob’ method implementation and use:

IMyLog log = UnityContainer.Resolve(typeof(IMyLog)) as IMyLog;

But ‘Resolve(Type t, …)’ is also an extension method…

Any thoughts are welcome.

P.S. Please note, that ‘my log’ object is created far-away from MyManager.DoJob…

  • 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-16T23:25:14+00:00Added an answer on May 16, 2026 at 11:25 pm

    Guess, I found most appropriate solution for test: It is not necessary to mock unity container and check if ‘log’ object was taken from it. I will just make a mock for ‘Log’ object, register its object instance in the container and check in test if this log object is really used.

    This will do what is required.

            Mock<IMyLog> mockLog = new Mock<IMyLog>();
            mockLog.Setup(mock=>mock.Id).Returns(TestLogId);
    
            IUnityContainer container = new UnityContainer();
            container
                .RegisterInstance(mockCommandExecutionLog.Object)
                ...
                ;
    
            ...
    
            mockLog.Verify(
                mock => mock.Id,
                Times.Once(),
                "It seems like 'Log' object is not used"
                );
    

    Thanks.

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

Sidebar

Related Questions

I've implemented some extension methods as defined here . I've put these into a
I'd like to create the context extension methods described in Cesar de la Torre's
I'm wondering whether I should create extension methods that apply on the object level
I have found good examples on how to create extension methods to read out
I've read at various websites that Thread.Abort is not very good to use. In
The CollectionViewSource.GetDefaultView() method is not in Silverlight 3. In WPF I have this extension
The extension methods: Response.AsJson Response.AsXml works fine when calling it from the constractor like:
I wonder if Linq extension methods are atomic? Or do I need to lock
I am using extension methods OrderBy and ThenBy to sort my custom collection on
I created a module to add some Extension Methods to a Class. This Class

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.