skip to Main Content

I have a function here that is for getting the logs in the queue. How can I mock this function for unit testing ?

void QueueBackgroundWorkItem(Func<CancellationToken, IServiceProvider, Task> workItem);
Mock<IBackgroundTaskQueue> _Mock= new Mock<IBackgroundTaskQueue>();
_Mock.Setup(x=>x.QueueBackgroundWorkItem(?)).Returns(Task.CompletedTask);

Thank you.

2

Answers


  1. One way to mock such method, if you are not testing a specific parameter, is to use an It.IsAny:

    _IBackgroundTaskQueueMock.Setup(x => x.QueueBackgroundWorkItem(It.IsAny<Func<CancellationToken, IServiceProvider, Task>>()))
        .Returns(Task.CompletedTask);
    
    Login or Signup to reply.
  2. QueueBackgroundWorkItem returns void so just setup with It.IsAny:

    _Mock.Setup(x => x.QueueBackgroundWorkItem(It.IsAny<Func<CancellationToken, IServiceProvider, Task>>()));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search