Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xunit - unit test with list or object as parameter

I am currently working at the unit tests of my REST API. I have a problem when I have a list or custom object as a parameter.

The first controller method has several parameters including a string List. I don't know how to add the values in the inline data for the List.

private static List<string> TestData()
{
    var testcase = new List<string>();
    testcase.Add("DEV");
    testcase.Add("IT");
    return testcase;
}

[Theory]
[InlineData(0, 10, TestData, "", 3)]
public async Task TestGetPersonItems(int pageNumber, int pageSize, List<string> departments, string filterText, int resultCount)
{
    using (API_DB_Context context = new API_DB_Context(_options))
    {
        // Arrange
        //List<string> departments = new List<string>();
        //departments.Add("DEV");
        List<string> locations = new List<string>();

        PersonenController controller = new PersonenController(context, _mapper);

        // Act
        var controllerResponse = await controller.Get(pageNumber, pageSize, departments, locations, filterText);

        // Assert
        if (resultCount > 0)
        {
            var objectResult = Assert.IsAssignableFrom<ObjectResult>(controllerResponse);
            ICollection<PersonDTO> model = Assert.IsAssignableFrom<ICollection<PersonDTO>>(objectResult.Value);
            Assert.Equal(resultCount, model.Count);
        }
        else
        {
            Assert.IsType<NotFoundObjectResult>(controllerResponse);
            var objectResponse = controllerResponse as ObjectResult;
            Assert.Equal(404, objectResponse.StatusCode);
        }
    }
}

For the second controller method I have an custom object and an integer as parameters. When I start test second test, i get the error "Could not find public static member (property, field, or method) named 'SearchData'"

public static IEnumerable<object[]> SearchData()
{
    yield return new object[] {
        new SearchDTO
        {
          searchText = "",
          page = 0,
          pageSize = 10
        }
    };
}

[Theory]
[MemberData(nameof(SearchData), 3)]
public async Task TestSearchPersonItems(SearchDTO searchDTO, int resultCount)
{
    using (API_DB_Context context = new API_DB_Context(_options))
    {
        // Arrange
        PersonenController controller = new PersonenController(context, _mapper);

        // Act
        var controllerResponse = await controller.SearchPersons(searchDTO);

        // Assert
        if (resultCount > 0)
        {
            var objectResult = Assert.IsAssignableFrom<ObjectResult>(controllerResponse);
            ICollection<PersonDTO> model = Assert.IsAssignableFrom<ICollection<PersonDTO>>(objectResult.Value);
            Assert.Equal(resultCount, model.Count);
        }
        else
        {
            Assert.IsType<NotFoundObjectResult>(controllerResponse);
            var objectResponse = controllerResponse as ObjectResult;
            Assert.Equal(404, objectResponse.StatusCode);
        }
    }
}

What do I have to do differently or what can I do better?

Thank you in advance!

like image 841
Henning Avatar asked Oct 11 '25 14:10

Henning


1 Answers

For the first UnitTest:

    public static IEnumerable<object[]> TestGetPersonItemsData =>
        new List<object[]>
        {
            new object[] { 0, 10, new List<string> { "DEV", "IT" }, "", 3 }
        };

    [Theory]
    [MemberData(nameof(TestGetPersonItemsData))]
    public async Task TestGetPersonItems(int pageNumber, int pageSize, List<string> departments, string filterText, int resultCount)
    {
        // Test
    }

And for the second write it like this:

    public static IEnumerable<object[]> TestSearchPersonItemsData =>
        new List<object[]>
        {
            new object[] { new SearchDTO { searchText = "", page = 0, pageSize = 10 }, 3 },
            new object[] { new SearchDTO { searchText = "test", page = 1, pageSize = 10 }, 1 }
        };

    [Theory]
    [MemberData(nameof(TestSearchPersonItemsData))]
    public async Task TestSearchPersonItems(SearchDTO searchDTO, int resultCount)
    {
        // Test
    }
like image 80
Andreas Avatar answered Oct 14 '25 07:10

Andreas