-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Mocking for Unit Tests #3086
Comments
It already works like that - |
with regards to persistence methods - you can mock them on IStorage instance.
Agree on mocking primary key retrievals - we need them too |
@centur the approach you suggested seems to require the use of a grain base class, which brings in a lot more concerns than it solves. Your constructor would need to accept all the parameters you specified. I did notice that storage providers implement Are there any additional suggestions on how storage mocking can be solved? |
@dandago umm, aren't you inheriting from We are mocking it using AutoSubstitute container so it works for us just fine. Yes, you can't inject IStorageProvider in your runtime scenario from DI-container, but why would you need exactly that semantic ? For unit tests you're testing a unit of code and it's expected behaviour, not how it's exactly constructed... We haven't started using DI for all the grains (our runtime ctors are parameter-less ones), but besides aestetical side of the code - what is not right with the case when your testing ctor (internal) accepts 4 more arguments to fully isolate the unit of code you're testing ? |
@centur I only meant that I am not using that constructor with 4 parameters. I am trying to figure out how to mock storage but that constructor brings in even more stuff that I don't know how to fill in, and shouldn't have to because it's not part of my production code. |
Testing something that is tightly coupled with the code out of your control is always dirty. But out of 4 parameters, 3 of them you will need for various aspects: It's not the cleanest possible code, really, we have bunch of test helpers, mockers and god objects. But it allowed us (with some help of handy libraries) to run unit tests of grains for 2 years of using Orleans. So if you want to test your grains - this would work. If you already can successfully test your grains and looking for some better practice - this is fair point to seek for different options. PS: |
Hey... Hopefully adding to this rather than hijacking here but I am currently having issues regarding Timers. After registering a timer I would like to test that it got fired. I believe this to be currently impossible. It would be nice if this could be mocked or tested someway |
So far, my experience is as follows:
|
By auto-injected you mean 1.4 injection into ctor ? |
@centur Yes, I mean that it is registered in DI by the runtime so you can accept it in ctor.
Not sure I've got that. You just mock those interfaces in your unit tests. Just be sure that the grain code uses those interfaces instead of calling inherited methods from |
Have you guys looked at #2856? it's supposed to help with a lot of these services that are really scoped to a particular grain activation. Since you are fighting these issues already, I would very much appreciate some feedback on the PR, and validate if it is helpful. It's been de-prioritized to after 1.5 since we didn't hear much feedback from others actually fighting against our current DI |
@DixonDs I thought that those interface instances are somehow hidden, found them on IGrainRuntime so it can be mocked along with IGrainRuntime. Does Orleans registers those interfaces ( @jdom haven't really checked it. Will take a look |
@centur Yes, Orleans registers them in DI: orleans/src/OrleansRuntime/Silo/Silo.cs Line 229 in 5fc9368
|
This is a question, a suggestion to add more detail to the docs with regards to unit testing, and also a call to consider possible API changes that would facilitate mocking grain dependencies for unit tests.
(Context: I am talking about real unit tests, not test cluster.)
There are at least 3 things in grains which need to be mocked, lest they be problematic when it comes to unit tests (if you can think of others, please mention them):
GrainFactory
WriteStateAsync()
)Below are suggestions on tackling the above. If you have a better idea, please share it!
The text was updated successfully, but these errors were encountered: