OData / OData.Neo

111 stars 18 forks source link

Odata Unit Test Case #25

Closed ChiraagKataria closed 1 year ago

ChiraagKataria commented 2 years ago

Hi, I have been developing unit test cases for Odata that are based on parameters entered via the URL. While developing, I have come across the issue of how to provide the URL paramters via the MSTest collaborating with Odata enablequery attribute and AsQueryable method.

Kindly Help!

TehWardy commented 2 years ago

You need to be clear on what you intend to test and be sure you're not relying on the code you're testing to produce anything that the test uses to make it's decision.

Our general principle with OData is that we are matching syntax with result items from a query.

so ...

// given 
var query = "odata string"
var data = SomeQueryableSet();
var expectedResults = ManuallyBuiltResultSet();

// when 
var actualResults = CodeYouAreTesting(data, query);

// then
actualResults.Should().BeEquivilentTo(expectedResults);

So lets say you're testing OData's $select param.

// given
var query = "$select=Name";
var data = new[] 
{
    new { Id = 1, Name = "Paul" },
    new { Id = 2, Name = "Hassan" },   
    new { Id = 3, Name = "Sam" }
};
var expectedResults = new[] { "Paul", "Hassan", "Sam" };

// when
 var actualResults = yourLib.Query(data, query);

 // then 
 actualResults.Should().BeEquivilentTo(expectedResults);

For testing endpoints you simply need to build a test that constructs an instance of the app then call the endpoint when it's in a known state. Hassan has a ton of video based examples of this on his channel .. https://www.youtube.com/c/HassanHabib/search?query=accceptance%20test

Perhaps start with something like this and build up ... https://www.youtube.com/watch?v=NcGybsFRLO8

ChiraagKataria commented 2 years ago

Overall, I have this URL for instance, "https://localhost:port/odata/page1?filter=column eq 'value'&top=1&$skip=0"

I am able to run this with Postman, where my endpoint return is "return ok(result)" where result is the value from the context.Database_Table.AsQueryable()". So, from test method, I am unable to pass those URL paramters for filter, top or skip. Here, I want to understand if there is a method for which how can I pass those paramters from testmethod when i initialize the httpcontext and where the controller-method works basis this.

TehWardy commented 2 years ago

You need to write an acceptance test not a unit test.

TehWardy commented 1 year ago

I assume this is resolved as it seemed to be more of an in context general question. I'm going to close this, open a new issue if you have further questions.