Moq. Perform the action specified as parameter

How to mock the following method:

public class TimeService : ITimeService { public void SetDelyEvent(int interval, bool reset, Action action) { var timer = new Timer {Interval = interval, AutoReset = reset}; timer.Elapsed += (sender, args) => action(); timer.Start(); } } 

I want to call this ACTION.

 var stub = new Mock<ITimeService>(); stub .Setup(m => m.SetDelyEvent(100, false, ACTION)); 
+6
source share
1 answer

Just use the .Callback( method .Callback( to call the method that will run when your mock is executed, your callback function can be passed to the Action that was passed to the original method, all you have to do is execute the action in your callback.

  var stub = new Mock<ITimeService>(); stub .Setup(m => m.SetDelyEvent(It.IsAny<int>(), It.IsAny<bool>(), It.IsAny<Action>())) .Callback((int interval, bool reset, Action action) => action()); 
+9
source

Source: https://habr.com/ru/post/975910/


All Articles