I would like to access the finder object inside the unit test class in order to perform test cases, but I am pretty new to the service collections and async programming please help me out with some leads or solution.
// startup class
public static class Startup
{
public static IServiceCollection Configure(IServiceCollection services = null)
{
services = services ?? new ServiceCollection();
services.AddSingleton<ILongestRisingSequenceFinder, LongestRisingSequenceFinder>();
return services;
}
}
// Inside Unit test class we should be able to get the method data to check the test cases
public class UnitTests
{
[Theory]
[InlineData(new [] {4,3,5,8,5,0,0,-3}, new [] {3,5,8})]
public async Task CanFind(IEnumerable<int> data, IEnumerable<int> expected)
{
IEnumerable<int> actual = null;
// TODO: create the finder instance and get the actual result
actual.Should().Equal(expected);
}
}
As the commenters have noted, you are testing the class LongestRisingSequenceFinder
. Good unit tests dispense with other concerns, and test one thing. Instead, skip service resolution, which is a completely separate concern.
Your test should look like
[Theory]
[InlineData(new [] {4,3,5,8,5,0,0,-3}, new [] {3,5,8})]
public async Task CanFind(IEnumerable<int> data, IEnumerable<int> expected)
{
var finder = new LongestRisingSequenceFinder();
// a guess on usage...
var actual = finder.MethodOfFinder(...);
actual.Should().Equal(expected);
}
If you're truly wanting to test service resolution with a call to the method, this is an integration test. A test like that is going to require constructing a test server, doing all the dependency injection, etc. in the context of the server.
Simply creating an artificial container as your code shows and then calling the method adds no value to testing. Think about it this way; your code was creating a container with 1 entry for the sole purpose of getting an entry. That's a really complicated way to instead just do new
in your test, so adds no value.