You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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/
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 mockConnection
and mockTransaction
to see ifConnection.CreateWriteTransaction
orTransaction.SetRangeInHash
are called. However it looks like I'm getting aNoOpContext
since my test doesn't callUseConsole
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.
The text was updated successfully, but these errors were encountered: