/// <summary> /// Checks the last method invocation on the mock; /// if Add was invoked the unexpected match is set up; /// if GetOrAdd or GetOrAddAsync was invoked the unexpected match is set up and the addItemFactory result will be /// returned; /// otherwise the default value for the specified type will be returned. /// </summary> /// <param name="call"></param> /// <returns> /// if GetOrAdd or GetOrAddAsync was invoked the unexpected match is set up and the addItemFactory result will be /// returned; /// otherwise the default value for the specified type will be returned if the last method invocation has a return /// type. /// </returns> public RouteAction Handle(ICall call) { Logger.LogDebug("NoSetUpHandler invoked"); var methodInfo = call.GetMethodInfo(); var args = call.GetArguments(); if (methodInfo.Name.Equals("Add")) { //We have everything we need to set up a match, so let's do it var key = args[0].ToString(); var value = args[1]; ProjectReflectionShortcuts.SetUpCacheEntryMethod(value.GetType()).Invoke(null, new[] { _mockedCachingService, key, value }); return(RouteAction.Return(null)); } if (methodInfo.Name.Equals("GetOrAdd")) { //We have everything we need to set up a match, so let's do it var key = args[0].ToString(); var value = args[1].GetType().GetMethod("Invoke").Invoke(args[1], new object[] { new CacheEntryFake(key) }); ProjectReflectionShortcuts.SetUpCacheEntryMethod(value.GetType()).Invoke(null, new[] { _mockedCachingService, key, value }); return(RouteAction.Return(value)); } if (methodInfo.Name.Equals("GetOrAddAsync")) { //We have everything we need to set up a match, so let's do it var key = args[0].ToString(); var task = args[1].GetType().GetMethod("Invoke").Invoke(args[1], new object[] { new CacheEntryFake(key) }); var taskResult = task.GetType().GetProperty("Result").GetValue(task); ProjectReflectionShortcuts.SetUpCacheEntryMethod(taskResult.GetType()).Invoke(null, new[] { _mockedCachingService, key, taskResult }); return(RouteAction.Return(task)); } //void method if (methodInfo.ReturnType == typeof(void)) { return(RouteAction.Return(null)); } //Return default values if (methodInfo.ReturnType.IsGenericType && methodInfo.ReturnType.GetGenericTypeDefinition() == typeof(Task <>)) { var genericArgument = methodInfo.ReturnType.GetGenericArguments().Single(); var defaultValue = genericArgument.GetDefaultValue(); return(RouteAction.Return(CoreReflectionShortcuts.TaskFromResultMethod(genericArgument).Invoke(null, new[] { defaultValue }))); } return(RouteAction.Return(methodInfo.ReturnType.GetDefaultValue())); }
internal static IAppCache SetUpCacheEntryRemove <T>(this IAppCache mockedCachingService, string cacheEntryKey) { EnsureArgument.IsNotNull(mockedCachingService, nameof(mockedCachingService)); EnsureArgument.IsNotNullOrEmpty(cacheEntryKey, nameof(cacheEntryKey)); Logger.LogDebug("Setting up cache entry Remove for '{cacheEntryKey}'", cacheEntryKey); mockedCachingService.When(x => x.Remove(Arg.Is <string>(s => s.Equals(cacheEntryKey)))) .Do(x => { Logger.LogDebug("Cache Remove invoked"); ProjectReflectionShortcuts.SetUpCacheEntryGetMethod(typeof(T)).Invoke(null, new object[] { mockedCachingService, cacheEntryKey, default(T) }); }); return(mockedCachingService); }
internal static IAppCache SetUpCacheEntryAdd <T>(this IAppCache mockedCachingService, string cacheEntryKey) { EnsureArgument.IsNotNull(mockedCachingService, nameof(mockedCachingService)); EnsureArgument.IsNotNullOrEmpty(cacheEntryKey, nameof(cacheEntryKey)); Logger.LogDebug("Setting up cache entry Add for '{cacheEntryKey}'", cacheEntryKey); mockedCachingService.When(x => x.Add(Arg.Is <string>(s => s.Equals(cacheEntryKey)), Arg.Any <T>(), Arg.Any <MemoryCacheEntryOptions>())) .Do(x => { Logger.LogDebug("Cache Add invoked"); //x provides the args as objects; this means we have to use reflection to set the item type for the SetUpCacheEntryGet method var args = x.Args(); var key = args[0].ToString(); var value = args[1]; ProjectReflectionShortcuts.SetUpCacheEntryGetMethod(value.GetType()).Invoke(null, new[] { mockedCachingService, key, value }); }); return(mockedCachingService); }