How to Mock (with Moq) Unity methods

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();

        ... 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...

13
задан Community 23 May 2017 в 11:47
поделиться