dros
dros

Reputation: 1567

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());
    }

Upvotes: 2

Views: 1891

Answers (2)

michasaucer
michasaucer

Reputation: 5238

I belive this package can help you:

https://github.com/shouldly/shouldly

Assertion can be tricky sometimes to understand what is going on. Shouldly make asserts easier.

With Shouldly you can make checking types like this:

yourList.ShouldBeOfType<List<string>>();

if type of yourList matched List<string> it will return true. If not, false. You can install Shouldly from nuget as well.

With this package you can refactor Assert.Equal to something like:

yourObject.yourProperty.ShouldBe("Some Stirng To Compare 'yourProperty;");

and example with int:

yourInteger.ShouldBe(10);

Upvotes: 2

Lajos Arpad
Lajos Arpad

Reputation: 76943

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.

Upvotes: 2

Related Questions