2016-08-03 168 views
0

我该如何模拟Azure Redis缓存?我该如何模拟Azure Redis缓存?

我想为使用Azure Redis缓存的应用程序编写单元测试。由于我在编写单元测试代码时嘲笑和写作是全新的,因此我正在寻找关于如何开始使用基本脚本来模拟/存储缓存组件的帮助。

回答

2

使用外部资源(如数据库,文件和缓存)进行测试是集成测试,非单元。你可以在单元测试中测试的事实是,你的代码正在调用缓存方法。

所以,首先,你需要一个接口缓存服务。这个接口不仅可以让你测试你的代码,还可以让你使用不同的缓存服务器。

public interface ICache 
{ 
    void Add<T>(string key, TimeSpan lifetime, T value); 

    bool TryGet<T>(string key, out T value); 

    void Remove(string key); 

    . . . 
} 

其次,您需要域代码来测试:

public class SleepingMembersService 
{ 
    private readonly TimeStamp _lifetime = TimeStamp.FromMinutes(5); 
    private readonly ICache _cache; 
    private readonly INotifier _notifier; 

    public SleepingMembersService(ICache cache, INotifier notifier) 
    { 
     _cache = cache; 
     _notifier = notifier; 
    } 

    private string MakeKey(User user) => $"unsleepingUser{user.Id}"; 

    public void WakeUpIfSleep(IUser user) 
    { 
     var key = MakeKey(user); 
     bool isWaking; 
     if (_cache.TryGet(key, out isWaking) && isWaking) 
      return; 

     notifier.Notify(user.Id, "Wake up!");    
    } 

    public void ConfirmImNotSleeping(IUser user) 
    { 
     var key = MakeKey(user); 
     _cache.Add(key, _lifeTime, true); 
    } 
} 

第三,让我们做桩缓存:

public class StubCache : ICache 
{ 
    public bool TryGetResult { get; set; } 

    public bool TryGetValue { get; set; } 

    public bool AddValue { get; set; } 

    public TimeStamp LifeTimeValue { get; set; } 

    void Add<T>(string key, TimeSpan lifetime, T value) 
    { 
     LifeTimeValue = lifetime; 
     AddValue = (bool)(object)value; 
    } 

    bool TryGet<T>(string key, out T value) 
    { 
     value = (T)(object)TryGetValue; 
     return TryGetResult; 
    } 

    . . . 
} 

最后,你可以编写单元测试:

pubic void ConfirmImNotSleeping_WhenCalled_CallsAdd() 
{ 
    var cache = new StubCache<bool>(); 
    var notifier = new StubNotifier(); 
    var service = new SleepingMembersService(cache, notifier); 
    var user = new StubUser(1, "John Doe"); 

    service.ConfirmNotSleeping(user); 

    Assert.IsTrue(cache.AddValue); 
} 

那么,你已经检查过该方法ConfirmNotSleeping调用方法Add。现在

,你应该实现ICache对Redis的:

public RedisCache : ICache 
{ 
    private IConnectionMultiplexer connection; 

    public bool TryGet<T>(string key, out T value) 
    { 
     var cache = Connection.GetDatabase(); 

     var rValue = cache.StringGet(key); 
     if (!rValue.HasValue) 
     { 
      value = default(T); 
      return false; 
     } 

     value = JsonConvert.DeserializeObject<T>(rValue); 
     return true; 
    } 

    . . .  
} 

为了简化实现存根和嘲笑,你可以使用库像Moq。这些库让你自动生成存根和嘲笑,以达到你的目的。所以你的测试代码将如下所示:

pubic void ConfirmImNotSleeping_WhenCalled_CallsAdd() 
{ 
    var cacheStub = new Mock<ICache>(); 
    var notifierStub = new Mock<INotifier>(); 
    var service = new SleepingMembersService(cache.Object, notifier.Object); 
    var userStub = new Mock<IUser>(); 

    service.ConfirmNotSleeping(user.Object); 

    cacheStub.Vertify(x => x.Add(It.IsAny<string>(), It.IsAny<TimeStamp>(), true)); 
} 
+0

谢谢你的帮助! 但是,第三步是给出编译错误 - void Add (string key,TimeSpan lifetime,T value) LifeTimeValue = lifetime; AddValue = value; } 由于值的类型是T. –

+0

如果您可以给出一个简单的set示例并从Redis缓存中获取对象并随后获得测试代码,那将是非常棒的。谢谢! –

+0

我纠正了方法'Add'和'TryGet'的执行错误。关于你的第二个评论:我的回答描述了缓存逻辑的单元问题。如果你想在测试中调用Redis方法,将会有集成测试,而不是单元测试。 –