C# Moq:如何模拟以函数回调作为参数的方法

C# Moq:如何模拟以函数回调作为参数的方法,c#,mocking,moq,C#,Mocking,Moq,我试图在InMemoryCache类中模拟以下GetOrSet方法 public class InMemoryCache : ICacheService { public T GetOrSet<T>(string cacheKey, Func<T> getItemCallback) where T : class { T item = MemoryCache.Default.Get(cacheKey) as T; if (i

我试图在InMemoryCache类中模拟以下GetOrSet方法

public class InMemoryCache : ICacheService
{
    public T GetOrSet<T>(string cacheKey, Func<T> getItemCallback) where T : class
    {
        T item = MemoryCache.Default.Get(cacheKey) as T;
        if (item == null)
        {
            item = getItemCallback();

            DateTime expireDateTime = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 4, 0, 0).AddDays(1);
            MemoryCache.Default.Add(cacheKey, item, expireDateTime);
        }
        return item;
    }
}
mockCacheService.Setup(x => x.GetOrSet(It.IsAny<string>(), It.IsAny<Func<object>>()))
                .Returns(new Dictionary<string, string> { { "US", "USA"} });

这取决于你想测试什么。以下是几个例子:

mockCacheService.Setup(x => x.GetOrSet(It.IsAny<string>(), It.IsAny<Func<object>>()))
                .Returns(new Dictionary<string, string> { { "US", "USA"} });
var mockCacheService = new Mock<ICacheService>();

// Setup the GetOrSet method to take any string as its first parameter and 
// any func which returns string as the 2nd parameter
// When the GetOrSet method is called with the above restrictions, return "someObject"
mockCacheService.Setup( x => x.GetOrSet( It.IsAny<string>(), It.IsAny<Func<string>>() ) )
   .Returns( "someObject" );

// Setup the GetOrSet method and only when the first parameter argument is "Key1" and 
// the second argument is a func which returns "item returned by func"
// then this method should return "someOtherObject"
mockCacheService.Setup( x => x.GetOrSet( "Key1", () => "item returned by func") )
   .Returns( "someOtherObject" );
编辑1

mockCacheService.Setup(x => x.GetOrSet(It.IsAny<string>(), It.IsAny<Func<object>>()))
                .Returns(new Dictionary<string, string> { { "US", "USA"} });
这很重要:

mockCacheService.Setup(x => x.GetOrSet(It.IsAny<string>(), It.IsAny<Func<object>>()))
                .Returns(new Dictionary<string, string> { { "US", "USA"} });

您可能知道这一点,但我希望您不要使用它来测试memorycache.GetOrSet方法中的
。这里的想法是,您正在测试另一个类,该类在特定条件下,最好使用上面的
setup
方法中的特定设置调用此mock。如果被测试的类没有使用“Key1”调用mock,并且没有传递返回“item retuned by func”的func,那么测试将失败。这意味着你被测试的类的逻辑是错误的。不是这门课,因为你在嘲笑它

这取决于你想测试什么。以下是几个例子:

mockCacheService.Setup(x => x.GetOrSet(It.IsAny<string>(), It.IsAny<Func<object>>()))
                .Returns(new Dictionary<string, string> { { "US", "USA"} });
var mockCacheService = new Mock<ICacheService>();

// Setup the GetOrSet method to take any string as its first parameter and 
// any func which returns string as the 2nd parameter
// When the GetOrSet method is called with the above restrictions, return "someObject"
mockCacheService.Setup( x => x.GetOrSet( It.IsAny<string>(), It.IsAny<Func<string>>() ) )
   .Returns( "someObject" );

// Setup the GetOrSet method and only when the first parameter argument is "Key1" and 
// the second argument is a func which returns "item returned by func"
// then this method should return "someOtherObject"
mockCacheService.Setup( x => x.GetOrSet( "Key1", () => "item returned by func") )
   .Returns( "someOtherObject" );
编辑1

mockCacheService.Setup(x => x.GetOrSet(It.IsAny<string>(), It.IsAny<Func<object>>()))
                .Returns(new Dictionary<string, string> { { "US", "USA"} });
这很重要:

mockCacheService.Setup(x => x.GetOrSet(It.IsAny<string>(), It.IsAny<Func<object>>()))
                .Returns(new Dictionary<string, string> { { "US", "USA"} });

您可能知道这一点,但我希望您不要使用它来测试memorycache.GetOrSet
方法中的
。这里的想法是,您正在测试另一个类,该类在特定条件下,最好使用上面的
setup
方法中的特定设置调用此mock。如果被测试的类没有使用“Key1”调用mock,并且没有传递返回“item retuned by func”的func,那么测试将失败。这意味着你被测试的类的逻辑是错误的。不是这门课,因为你在嘲笑它

期望的行为是什么。使用
It.IsAny()
。如果传递给方法的值为空,您还应该对函数执行空检查null@Nkosi请看我上面的更新帖子什么是期望的行为。使用
It.IsAny()
。如果传递给方法的值为空,您还应该对函数执行空检查null@Nkosi请参阅上面我的更新帖子,这对我有用var mockCacheService=new Mock();mockCacheService.Setup(x=>x.GetOrSet(“CountriesDictionary”,It.IsAny())).Returns(新字典{{“US”,“USA”});我很高兴一切顺利。请查看我的编辑,因为这非常重要;mockCacheService.Setup(x=>x.GetOrSet(“CountriesDictionary”,It.IsAny())).Returns(新字典{{“US”,“USA”});我很高兴一切顺利。请看我的编辑,因为这真的很重要。
mockCacheService.Setup(x => x.GetOrSet(It.IsAny<string>(), It.IsAny<Func<object>>()))
                .Returns(new Dictionary<string, string> { { "US", "USA"} });