2017-11-01 6 views
0

다음 구현이 있습니다. 연결 문자열을 제공하고 싶지 않기 때문에 조롱을 통해 이들에 대한 단위 테스트를 작성하고 싶습니다.Moq Redis 캐시의 단위 테스트

public virtual void Put<TValue>(string key, TValue value, TimeSpan? timeout) 
     {   
      var serialized = JsonConvert.SerializeObject(value); 
      if (Config.CacheEnableCompression) 
      { 
       this.Cache.StringSet(key, Compressor.GZipCompress(serialized), timeout ?? DefaultExpiration); 
      } 
      else 
      { 
       this.Cache.StringSet(key, serialized, timeout ?? DefaultExpiration); 
      } 
     } 
+0

캐시/구성 객체의 유형은 무엇입니까? – Seb

+0

@Seb 문자열 유형 – Keys

+0

@Seb 기본적으로 Config는 구성 관리자이며 Cache는 connectionmultiplexer.getdatabase()에서 파생됩니다. – Keys

답변

1

당신은 멀티플렉서와 ​​같은 데이터베이스 개체 조롱 수 :

var mockDatabase = new Mock<StackExchange.Redis.IDatabase>(); 

var mockMultiplexer = new Mock<StackExchange.Redis.IConnectionMultiplexer>(); 

mockMultiplexer 
    .Setup(_ => _.GetDatabase(It.IsAny<int>(), It.IsAny<object>())) 
    .Returns(mockDatabase.Object);