Reputation: 201
I'm trying to write some unit test around AWS DynamoDb C# library, CreateBatchWrite.
This is the method:
public void BatchSave<T>(string tableName, List<T> items)
{
if (string.IsNullOrWhiteSpace(tableName))
{
throw new ArgumentNullException(nameof(tableName));
}
if (items == null)
{
throw new ArgumentNullException(nameof(items));
}
items.RemoveAll(x => x == null);
BatchWrite<T> batch = _dynamoDbContext.CreateBatchWrite<T>(
new DynamoDBOperationConfig()
{
OverrideTableName = tableName
});
batch.AddPutItems(items);
batch.Execute();
}
My problem is the return object from CreateBatchWrite: BatchWrite is a strongly typed class.
Apart from moving my preconditions one layer below or above, is there any way of unit testing this? Is my approach correct?
Upvotes: 2
Views: 1111
Reputation: 247471
If you only want to test preconditions then create an instance of the class under test, pass arguments that will cause the expected behavior and assert that it happens as expected.
There is no need to even mock the dependency if it is not needed for the test to be exercised to completion.
For example
[TestClass]
public class AwsTests {
[Test]
public void Should_Throw_For_Null_TableName() {
//Arrange
var subject = new SubjectUnderTest(null);
ArgumentNullException exception = null;
var expected = "tableName";
//Act
try {
subject.BatchSave<object>(null, null);
} catch (ArgumentNullException e) {
exception = e;
}
//Assert
exception.Should().NotBeNull();
exception.ParamName.Should().Be(expected);
}
[Test]
public void Should_Throw_For_Null_Items() {
//Arrange
var subject = new SubjectUnderTest(null);
ArgumentNullException exception = null;
var expected = "items";
//Act
try {
subject.BatchSave<object>("fakeTableName", null);
} catch (ArgumentNullException e) {
exception = e;
}
//Assert
exception.Should().NotBeNull();
exception.ParamName.Should().Be(expected);
}
}
The above tests the two if
conditions of the method as isolation unit tests by providing only what is necessary for the test to be safely exercised to completion.
Upvotes: 1