Search code examples
c#tddxunit

XUnit test to return a see if a list is being returned


I'm learning Unit Testing and I'm struggling to grasp how we can test to see if a type of list is returning, not necessarily the content of the list but to make sure its a LIST that is being returned.

Returning an empty list of strings

    public List<string> GetList()
    {
        var names = new List<string>();

        return names;
    }

My test, trying to return a typeofList:

    [Fact]
    public void GetListTest()
    {
        Assert.Equal(typeof(List<string>), GetList());
    }

Solution

  • Here

    Assert.Equal(typeof(List<string>), GetList());
    

    you are testing whether the type of string list is equal with the actual list. You are comparing apples with oranges. You can do this:

    Assert.Equal(typeof(List<string>), GetList().GetType());
    

    Also, you can construct composite logical criteria and assert equal to those, so you can check whether the type is the expected one and empty in the same test.