This is a migrated thread and some comments may be shown as answers.

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

1 Answer 126 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Uriah
Top achievements
Rank 1
Uriah asked on 10 Sep 2013, 01:38 PM
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),
    DateTime.Now.AddDays(1),
    Cache.NoSlidingExpiration
    );
 
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.

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 10 Sep 2013, 02:52 PM
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?

Regards,
Stefan
Telerik
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.
Tags
General Discussions
Asked by
Uriah
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or