I am using Hangfire to schedule some jobs. All my jobs have a schedule date, so I’m using Schedule static extension method
public static string Schedule([NotNull] this IBackgroundJobClient client, [InstantHandle][NotNull] Expression<Action> methodCall, DateTimeOffset enqueueAt);
I want to unit test my methods, so that I can check that Hangfire is actually being triggered.
To do so, I’ve followed Hangfire’s documentation on unit testing (https://docs.hangfire.io/en/latest/background-methods/writing-unit-tests.html) which suggests mocking IBackgroundJobClient
and verifying on the only public Create method it has:
string Create([NotNull] Job job, [NotNull] IState state);
But I can see that for Schedule invocations, this is not being called, another overload that is not public is being called instead, so I get the following error:
Message: Moq.MockException :
Expected invocation on the mock at least once, but was never performed: x => x.Create(It.IsAny<job>(), It.IsAny<enqueuedstate>())
No setups configured.
Performed invocations:
IBackgroundJobClient.Create(JobService.TaskFunction, ScheduledState)
Nevertheless, I’ve made an attempt with Enqueue
method, and that seems to work with the mock. But I need to use Schedule
.
So how can I unit test Hangfire for Schedule
method?