How do you mock an IAsyncEnumerable?

后端 未结 4 1128
时光取名叫无心
时光取名叫无心 2020-12-20 13:23

I want to unit test a method that calls another method of a service returning an IAsyncEnumerable. I have created a a mock of my service Mock<

4条回答
  •  礼貌的吻别
    2020-12-20 14:28

    I recommend using ToAsyncEnumerable from System.Linq.Async, as Jeroen suggested. It seems like you're using Moq, so this would look like:

    async Task MyTest()
    {
      var mock = new Mock();
      var mockData = new[] { "first", "second" };
      mock.Setup(x => x.CallSomethingReturningAsyncStream()).Returns(mockData.ToAsyncEnumerable());
    
      var sut = new SystemUnderTest(mock.Object);
      var result = await sut.MyMethodIWantToTest();
    
      // TODO: verify `result`
    }
    

提交回复
热议问题