简体   繁体   中英

Unit test with multiple test cases, for method which returns different object types

I have static method:

public static ReturnBaseClass GetValue(SimpleBaseClass simpleBaseClass)
{
    ReturnBaseClass returnBaseClass = null;

    if (simpleBaseClass is simpleInheritedClass1)
    {
        returnBaseClass = new ReturnInheritedClass1();
    }
    else if (simpleBaseClass is simpleInheritedClass2)
    {
        returnBaseClass = new ReturnInheritedClass2();
    }
    else if (simpleBaseClass is simpleInheritedClass3)
    {
        returnBaseClass = new ReturnInheritedClass3();
    }

    return returnBaseClass;
}

According to type of object in method parameter (simpleBaseClass), method should return another type of object. This 'if else' branch is quite long and I wanna write simple unit test to test this. I am stuck with checking return object type. I wrote test method:

[TestCase(new simpleInheritedClass1(), Type ReturnInheritedClass1)]
[TestCase(new simpleInheritedClass2(), )]
[TestCase(new simpleInheritedClass3(), )]
public void GetValueTest(SimpleBaseClass simpleBaseClass, Type type)
{

}

What I did in first Test Case (Type ReturnInheritedClass1) does not work. I do not know how to forward type of object to my Unit test in this way ?

Try to use TestCaseSource attribute:

    [TestCaseSource("Source")]
    public void GetValueTest(SimpleBaseClass simpleBaseClass, ReturnInheritedClassBase simpleInheritedClass)
    {

    }

    private static readonly object[] Source = new object[]
    {
        new object[] { new SimpleInheritedClass1(), new ReturnInheritedClass1()},
        new object[] { new SimpleInheritedClass2(), new ReturnInheritedClass2()}
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM