5

I am new to writing test cases for WebAPI's. I have seen similar questions asked in the past, but not answered, but I am wondering how I would test my APIs if they have an ODataQueryOptions as part of the parameters. See below:

public IQueryable<Item> GetByIdAndLocale(ODataQueryOptions opts, 
                                         Guid actionuniqueid, 
                                         string actionsecondaryid)

Would I have to moq this? If so, how would this look? Any help would be appreciated.

2 Answers 2

7

For ODataQueryOptions perspective, you may want to test that all the OData query options can work with your Function. So firstly you need to create an instance of ODataQueryOptions. Here is an example:

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri);
ODataQueryContext context = new ODataQueryContext(EdmCoreModel.Instance, elementType);
ODataQueryOptions options = new ODataQueryOptions(context, request);

So you need to create your own EDM model to replace EdmCoreModel.Instance, and replace requestUri with your query. elemntType in ODataQueryContext is "The CLR type of the element of the collection being queried".

Sign up to request clarification or add additional context in comments.

1 Comment

As of version 6.0 of Microsoft.AspNet.OData, this no longer works and will throw an exception. To correct this, you need to add the following in order to instantiate the HttpRequestMessage slightly more robust: HttpConfiguration configuration = request.GetConfiguration(); if (configuration == null) { configuration = new HttpConfiguration(); request.SetConfiguration(configuration); } configuration.EnableDependencyInjection((Action<IContainerBuilder>)null); You'll also need to add this up top to access that extension method: using System.Web.OData.Extensions;
0

I cannot tell from the phrasing, but is the above call (GetByIdAndLocale) the Web API that you are trying to test or are you trying to test something that is calling it?

One uses a mock or a stub to replace dependencies in a Unit Under Test (UUT). If you are testing GetByIdAndLocale() then you would not mock it though if it calls something else that takes the ODataQueryOptions as a parameter, you could use Moq to stub/mock that call.

If you are testing some unit that calls GetByIdAndLocale() then, yes, you could moq it. How exactly you might do this depends upon the goal (checking that the correct values are being passed in vs. checking that the returned IQueryable is correctly processed) basically, matching against It.IsAny() or against some matcher.

Which do you want to test?
GetByIdAndLocale(), something that calls it or something (not shown) that it calls?

What are you interested in verifying?
Correct options are passed in or the processing of the return from the mocked call?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.