Can't mock method that takes Func<Dictionary<string,string>>

2 posts, 0 answers
  1. Uriah
    Uriah avatar
    1 posts
    Member since:
    Apr 2013

    Posted 10 Sep 2013 Link to this post

    Here is my attempt to mock the interface (I actually want to return a value, but throw just to make it easier to identify):

    var cache = Mock.Create<ICache>();
    _cacheFactoryMock.Arrange(m => m.Application).Returns(cache);
    cache.Arrange(m => m.GetOrAdd(Arg.AnyString, Arg.IsAny<Func<Dictionary<string, string>>>, Arg.IsAny<DateTime>(), Arg.IsAny<TimeSpan>(), null)).Throws(new DivideByZeroException());

    Here is the invocation:
    return _caches.Application.GetOrAdd(
        string.Format(CacheKeyFormat, groupName),
        () => _ldapService.FindLdapUserEmailsInGroup(groupName),
    Here is the method signature of FindLdapUserEmailsInGroup:
    Dictionary<string, string> FindLdapUserEmailsInGroup(string groupName);

    The pertinent part of the cachefactory interface:
    public interface ICacheFactory
            ICache Application { get; }       

    The pertinent part of ICache:
    public interface ICache
             TItem GetOrAdd<TItem, TState>(string key, Func<TState, TItem> getValue, TState state, DateTime absoluteExpiration, TimeSpan slidingExpiration, object dependencies = default(object));
            TItem GetOrAdd<TItem>(string key, Func<TItem> getValue, DateTime absoluteExpiration, TimeSpan slidingExpiration, object dependencies = default(object));

    I'm not sure where I am going wrong.  I have tried several permutations of the dependencies object (using null, default etc), and I'm not sure what could be wrong with the Func<Dictionary<string,string>> arg but nothing I have done so far is getting the arrange to take effect.  Any guidance is appreciated.
  2. Stefan
    Stefan avatar
    198 posts

    Posted 10 Sep 2013 Link to this post

    Hello Uriah,

    It's hard to tell what might be wrong. I see you arrange _cacheFactoryMock.Arrange(m => m.Application)but then use  _caches.Application - it's hard to tell if they're related. Could you wrap the all the bits into a small repro project and attach it to this ticket for investigation, please?

    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  3. DevCraft R3 2016 release webinar banner
Back to Top