-3

I need your help on testing a unit which includes a redis subscription block. I tried so many variations but cant find a compile-error-free test code :( . How would you test the method below with xunit ? (btw I am a newbie in testing and I try to get this testing mindset asap)

   public async Task MethodAsync(ISequencer sequencer, CancellationToken token)
    {
        if (this.redis == null)
        {
            return;
        }

        var mnemonicMapping = this.redisRetrieverConfiguration.MnemonicList.ToDictionary(x => x.InputMnemonic.ToLower(), x => x.OutputMnemonic);

        var channel = new RedisChannel(this.redisRetrieverConfiguration.RedisChannel, RedisChannel.PatternMode.Auto);

        this.logger.LogInformation("Subscribing to Redis channel - {RedisChannel}", this.redisRetrieverConfiguration.RedisChannel);

        await this.redis.GetSubscriber().SubscribeAsync(channel, (ch, val) =>
        {
            var liveData = this.ParseRedisValueJsonToMnemonicCollection(val, mnemonicMapping);
            sequencer.RealtimeMnemonicsDataArrived(liveData);
        }).ConfigureAwait(false);
    } 

here is my testing code :)

 public async Task MethodAsync_Works()
    {
        MnemonicMapping mappingValue = new MnemonicMapping() { InputMnemonic = "1111", OutputMnemonic = "2222" };
        var options = Options.Create(new RedisRetrieverConfiguration() { Address = string.Empty, AssetId = 12, RedisChannelPrefix = "channel/",
            MnemonicList = new MnemonicMapping[] { mappingValue } });
        Mock<ILogger<MnemonicsDataRetriever>> mockLogger = new();
        var logger = mockLogger.Object;
        Mock<IConnectionMultiplexer> mockConnection = new();
        var connection = mockConnection.Object;
        
        Mock<ISequencer> mockSequencer = new();
        var sequencer = mockSequencer.Object;
        Mock<ISubscriber> mockSubscriber = new();
        var subscriber = mockSubscriber.Object;
        MnemonicsDataRetriever mnemonicsDataRetriever = new MnemonicsDataRetriever(options, logger, connection);

        // Here stucked with setting up connection and subscriber

        //mockConnection.Setup(x => x.GetSubscriber(It.IsAny<object>())).Returns(subscriber);
        //mockSubscriber.Setup(x => x.SubscribeAsync(It.IsAny<RedisChannel>(), CommandFlags.None))




        await mnemonicsDataRetriever.StartRetrievingRealtimeMnemonicsDataAsync(sequencer, default).ConfigureAwait(false);
        // here I will assert some other fields but I am stucked in above commented lines
    }
katmanco
  • 1,146
  • 12
  • 24

0 Answers0