Home > Software design >  Raising a Callback passed as a parameter in a method call using Mock
Raising a Callback passed as a parameter in a method call using Mock

Time:04-05

I have a method that takes a callback as a a parameter:

  public void GetAllAvailableNames(Action<List<string>> callback, Exchange? exchange = null, string symbol = null)
    {
        callback.Invoke(new List<string> {"test"});
    }

How can I raise the callback using mock, this is what I have tried among other things:

 this._marketDataClientMock
        .Setup(x => x.GetAllAvailableNames( It.IsAny<Action<List<string>>>(), It.IsAny<Exchange>(), It.IsAny<string>()))
        .Callback((List<string> x) => x = this.Vets);

CodePudding user response:

Capture the action parameter within the CallBack and invoke it manually

this._marketDataClientMock
    .Setup(x => x.GetAllAvailableNames( It.IsAny<Action<List<string>>>(), It.IsAny<Exchange>(), It.IsAny<string>()))
    .Callback((Action<List<string>> callback, Exchange? exchange, string symbol) => callback.Invoke(this.Vets));
  • Related