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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:30:24+00:00 2026-05-16T22:30:24+00:00

There is a function: public class MyCacheClass : ICache { public void T GetObject<T>(Func<T>

  • 0

There is a function:

public class MyCacheClass : ICache
{
    public void T GetObject<T>(Func<T> func)
    {
        T t;
        ...
        t = func();
        ...
        return t;
    }
}

public class MyWorkClass : IWork
{
    public Object MyWorkMethod(string value)
    {
        return new object();
    }
}

These functions are been called in the following way:

public class MyTestableClass 
{
    public void MyTestableFunc(ICache cache, IWorkClass work)
    {
        string strVal="...";
        ...
        Object obj = cache(()=>work.MyWorkMethod(strVal));
        ...
    }
}

It is necessary to write a UnitTest (with Moq) for that and check if proper parameter as passing into ‘MaCacheClass.GetObject’.

It should be something like this:

[TestMethod]
public void MyTest()
{
    Mock<ICache> mockCache = new Mock<ICache>();
    Mock<IWorkClass> mockWorkClass  = new Mock<IWorkClass>();

    MyTestableClass testable = new MyTestableClass();
    testable.MyTestableFunc(mockCache.Object, mockWorkClass.Object);

    // should I check if 'MyCacheClass' was called with proper parameter?
    mockCache.Verify(mock=>mock.GetObject(...)).Times.Once());
}

How could I provide parameter that will fit as ‘lambda-function’?
Are there any other options?

Any thoughts are welcome.

  • 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-16T22:30:25+00:00Added an answer on May 16, 2026 at 10:30 pm

    One of the option could be: instead on Funct parameter pass some object that implement interface with ‘GetObject’ function, for example:

    public interface IGetObject
    {
        T GetObject<T>();
    }
    
    public class MyGetObject : IGetObject
    {
        public MyGetObject()
        {
        }
    
        public void Init(string strVal, Func<string, T> func)
        {
            _strVal = strVal;
            _func = func;
        }
    
        public T GetObject<T>()
        {
            return _func(_strVal);
        }
    }
    
    public class MyCacheClass : ICache
    {
        public void T GetObject<T>(IGetObject getter)
        {
            T t;
            ...
            t = getter.GetObject<T>();
            ...
            return t;
        }
    }
    
    public class MyTestableClass 
    {
        public void MyTestableFunc(ICache cache, IWorkClass work)
        {
            string strVal="...";
            IGetObject getter = UnityContainer.Resolve<IGetObject>();
            getter.Init(strVal, str=>work.MyWorkMethod(str));
            ...
            Object obj = cache.GetObject(getter);
            ...
        }
    }
    

    In this case, I guess, we need also to inject UnityContainer into tested object, therefore it will be something like that:

    [TestMethod]
    public void MyTest()
    {
        Mock<ICache> mockCache = new Mock<ICache>();
        Mock<IWorkClass> mockWorkClass  = new Mock<IWorkClass>();
        Mock<IGetObject> mockGetter  = new Mock<IGetObject>();
    
        mockGetter.Setup(mock=>mock.GetObject()).Return(new Object());
    
        MyTestableClass testable = new MyTestableClass();
        testable.MyTestableFunc(mockCache.Object, mockWorkClass.Object);
    
        // should I check if 'MyCacheClass' was called with proper parameter?
        mockCache.Verify(mock=>mock.GetObject(mockGetter.Object)).Times.Once());
    }
    

    AND provide additional test for ‘GetObject’ method of ‘MyCacheClass’ that will check if it calls ‘GetObject’ method of the IGetObject parameter…

    P.S. A little bit complicated solution… so, if anybody see the better one, please advise!

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

Sidebar

Related Questions

My WCF service exposes this function public SerialNumberInfo GetSerialNumberInfo(string serialNumber) { } Is there
public function __construct($input = null) { if (empty($input)){ return false; } and then there's
There is probably something dumb I am doing wrong here. public function get_scores($id) {
Is there a way to get the following function declaration? public bool Foo<T>() where
public class ItemView extends MovieClip { private var _title:TextField; private var _extra:MovieClip; public function
I have the following function: public class InfrStadium { private InfrStadium(int iTeamId) { _teamId
HI all, my scenario public class Permission { public virtual Function Function { get;
Is there a way to access the Project object from a NAnt extension function,
I've got this Function: Public Class QueryStringUtil Public Shared Function GetQueryStringValue(Of T As Structure)(ByVal
I have 2 classes public class Customer{ ... public String getCustomerNumber(); ... } public

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.