Testing that a job uses Hangfire.Console
I'm trying to create a unit test for a hangfire job and want to verify that it writes to the console. However since this works with static extension methods I can't detect if the call happened or not. I've even tried using a mock PerformContext with a mock Connection and mock Transaction to see if Connection.CreateWriteTransaction or Transaction.SetRangeInHash are called. However it looks like I'm getting a NoOpContext since my test doesn't call UseConsole to use the console. I also don't see anyway I could call that and have it get picked up by the static methods.
Any help you can provide would be greatly appreciated.
I would like to know that too.
[..] since my test doesn't call
UseConsoleto use the console [..]
And we cannot add or access the console context because both ConsoleServerFilter and ConsoleContext are internal classes.
@zlangner I know this is an old issue, but did you find a way to get access in your unit-tests?
No. What I've started doing is use ILogger<> for logging and not the Hangfire Console. Using ILogger<>, it's fairly simple to tell if logging happened or not and you get structured logging to boot.
We still use the Hangfire Console but mostly just for progress bars or sparse messages. Things we are willing to skip the unit test.
That said, one could make your own object that wraps the Hangfire context and implements an interface that mirrors the static functions. Then using DI create this as a transient from the Hangfire context. Given that it's just an interface to the using class, it would be easy enough to make a mock for unit tests. Pros: you can unit test Cons: there is an extra object in the mix that you have to use instead of the handy extension methods. This is option 1 from https://methodpoet.com/unit-test-static-method/