I am trying to use xUnit Theory to complete a unit test under different scenarios I have function that returns the 1st instance of an object that meets a certain criteria.
public MyObject GetFirstObjectThatIsGzip(List<MyObject> obj){
return obj
.FirstOrDefault(x => x.Key.EndsWith(".gz"));
}
Unit test:
[Fact]
public async Task GivenAlist_WhenMoreThanOneObject_ReturnTheFirstWithCorrectFormat()
{
var objectToReturn = GetMyTestObjects();
_mockClient
.Setup(x => x.GetFirstObjectThatIsGzip(It.IsAny<MyObject>())
.ReturnsAsync(s3ObjectsToReturn);
//Act
var result = await _myService.GetDataAsync("myconnectionstring");
//Assert
Assert.Equal(result.Key, objectToReturnFirstOrDefault(x => x.Key.EndsWith(".gz")).Key);
}
MyObjects:
public static List<MyObject > GetTestMyObject()
{
return new List<MyObject >
{
new MyObject { Key = "ABC" },
new MyObject { Key = "abc.json" },
new MyObject { Key = "xzy.gz" },
new MyObject { Key = "lmno.gz" },
new MyObject { Key = "rstu.gz" }
};
}
I want to know how to pass different lists of objects to one parameter that can be the return object.
[Theory]
[MemberData(nameof(GetTestMyObject), MemberType = typeof(MyObject))
public async Task GivenAlist_WhenMoreThanOneObject_ReturnTheFirstWithCorrectFormat(List<MyObject> objs)
{
var objectToReturn = GetMyTestObjects();
_mockClient
.Setup(x => x.GetFirstObjectThatIsGzip(It.IsAny<MyObject>())
.ReturnsAsync(objs);
//Act
var result = await _myService.GetDataAsync("myconnectionstring");
//Assert
Assert.Equal(result.Key, objectToReturnFirstOrDefault(x => x.Key.EndsWith(".gz")).Key);
}
I know that the Data Generator must implement IEnumerable<object[]> Can I change the IEnumerable<object[]> to be IEnumerable<MyObject[]> Also the unit test seems to give an error saying that the number of input parameters must match the number of items in the data I am generating ?
Does this make sense ?