Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing Unit Tests with ODataQueryOptions

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.

like image 504
Greg P Avatar asked Jun 04 '14 18:06

Greg P


1 Answers

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".

like image 104
zoe Avatar answered Sep 28 '22 16:09

zoe