-
Notifications
You must be signed in to change notification settings - Fork 473
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
[feature/netcore] How to unit test with ODataQueryOptions #1352
Comments
In fact I see that you have a RequestFactory class to Create your HttpRequest object that ends up being quite nightmarish and uses internal methods that I don't have access to! Example:
|
RequestFactory exists so the UTs only need to create a request using the same code even though it's quite different under the hood. You can create a new DefaultHttpContext() and access the .Request property to create an HttpRequest in UTs. |
There's a fairly descriptive article about testing AspNetCore controllers here, this might offer some useful suggestions. |
@robward-ms thank you for the suggestions but I have tried using DefaultHttpContext() to get an HttpRequest but the problem is when creating the ODataQueryOptions object there is a lot of validation/requirements on the configuration of the HttpRequest object. For example eventually from ODataQueryOptions constructor I end up here:
So if I don't have RequestServices with IPerRouteContainer registered I get an exception, then there's more and more. I started also trying to use wrappers for ODataQueryOptions so I could create mocks but this ended up being too much work as well as I had to create a wrapper for SelectExpandQueryOption, then SelectExpandClause, then SelectItem, etc etc etc. Sorry for mentioning the controller, forget about that :). I'm really trying to unit test my own custom query validation methods that take ODataQueryOptions as it's only param. I imagine I could eventually get something to work, but I was just hoping there may be a easier path. |
@rmadisonhaynie - Ya, we have the same problem in our UTs. You could "borrow" the Request and Config factory classes form the UT abstraction project. As an alternative, we could look at creating a simply way to construct a ODataQueryOptions without a request but I think that is a UT-only scenario. |
I'll try to borrow your UT factory classes for now but yes I'd imagine for future .netcore OData users an easier way to unit test with ODataQueryOptions would be appreciated |
This may or may not be useful to you.
|
@freeranger yikes that's a lot of setup :). My setup up is a little different but I think I can adapt your example to get something to work for myself. In fact I didn't realize you could use ODataQueryOptions in an ActionFilter which is actually probably what I'll refactor to doing, so this is perfect thank you! |
Closing this thread for now. Please comment if new issues arise. |
@AlanWong-MS ok 👍🏻 Also just want to mention @freeranger I used your solution and it’s working great for me, thank you! |
No problem, glad I could help! |
I know it is old, I am trying to use the solution above but I cannot satisfy "using Microsoft.AspNetCore.Http.Internal;" with net core 3.1, I remember I used something similar for .net core 2.1. |
@freeranger , I want to hug you :) :) Finally I solved my unit test issue because of you. |
Hello, I'm wondering if someone could help me with how to unit test my controller methods that take ODataQueryOptions as a param.
My simpe controller:
I can find many examples of how to create an ODataQueryOptions object in OData WebApi 6.x but can't get it to work in Microsoft.AspNetCore.OData 7.0.0-beta2
Assemblies affected
Microsoft.AspNetCore.OData 7.0.0-beta2
Thank you
The text was updated successfully, but these errors were encountered: