Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing that a job uses Hangfire.Console #101

Open
zlangner opened this issue Sep 2, 2020 · 2 comments
Open

Testing that a job uses Hangfire.Console #101

zlangner opened this issue Sep 2, 2020 · 2 comments

Comments

@zlangner
Copy link

zlangner commented Sep 2, 2020

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.

@jerone
Copy link

jerone commented Aug 10, 2023

I would like to know that too.

[..] since my test doesn't call UseConsole to 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?

@zlangner
Copy link
Author

zlangner commented Aug 11, 2023

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants