E
Eric
I have a number of static helper methods to read in data from an
IDataReader; one of the services provided by this helper class is the
ability to return a nullable type as needed, for instance:
public static DateTime? GetNullableDateTime(object value) {...}
public static int? GetNullableInterger(object value) {...}
etc.
In writing tests for this class, all of the GetNullable type methods should
in fact return null if the value passed in is either null or DBNull.Value,
so you could have the following valid but cumbersome tests:
[Test]
public void DateTimeNullable_NullDateTimeIfNull() {
_assertNullDateTimeFor(null);
_assertNullDateTimeFor(DBNull.Value);
}
private static void _assertNullDateTimeFor(object value) {
Assert.That(DataReaderHelper.GetNullableDateTime(value), Is.Null); }
[Test]
public void IntegerNullable_NullIntegerIfNull() {
_assertNullIntegerFor(null);
_assertNullIntegerFor(DBNull.Value);
}
private static void _assertNullIntegerFor(object value) {
Assert.That(DataReaderHelper.GetNullableInteger(value), Is.Null); }
----------------------------
Too make the test class less cumbersome and noisy, I'd rather have one
assertion helper that can invoke whatever nullable method passed into it,
but I'm not getting my head around how to do this very well.
The assert method should be something like:
private static void
_assertNullReturnedForNullableEquivalents(delegateMethod dm) {
Assert.That(dm(null), Is.Null);
Assert.That(dm(DBNull), Is.Null);
}
The test method should look something like:
[Test]
public void DateTimeNullable_NullDateTimeIfNull() {
assertNullReturnedForNullableEquivalents(DataReaderHelper.GetNullableDateTime)
}
I need a delegate that looks something like
delegate T delegateMethod<T>(object value);
IDataReader; one of the services provided by this helper class is the
ability to return a nullable type as needed, for instance:
public static DateTime? GetNullableDateTime(object value) {...}
public static int? GetNullableInterger(object value) {...}
etc.
In writing tests for this class, all of the GetNullable type methods should
in fact return null if the value passed in is either null or DBNull.Value,
so you could have the following valid but cumbersome tests:
[Test]
public void DateTimeNullable_NullDateTimeIfNull() {
_assertNullDateTimeFor(null);
_assertNullDateTimeFor(DBNull.Value);
}
private static void _assertNullDateTimeFor(object value) {
Assert.That(DataReaderHelper.GetNullableDateTime(value), Is.Null); }
[Test]
public void IntegerNullable_NullIntegerIfNull() {
_assertNullIntegerFor(null);
_assertNullIntegerFor(DBNull.Value);
}
private static void _assertNullIntegerFor(object value) {
Assert.That(DataReaderHelper.GetNullableInteger(value), Is.Null); }
----------------------------
Too make the test class less cumbersome and noisy, I'd rather have one
assertion helper that can invoke whatever nullable method passed into it,
but I'm not getting my head around how to do this very well.
The assert method should be something like:
private static void
_assertNullReturnedForNullableEquivalents(delegateMethod dm) {
Assert.That(dm(null), Is.Null);
Assert.That(dm(DBNull), Is.Null);
}
The test method should look something like:
[Test]
public void DateTimeNullable_NullDateTimeIfNull() {
assertNullReturnedForNullableEquivalents(DataReaderHelper.GetNullableDateTime)
}
I need a delegate that looks something like
delegate T delegateMethod<T>(object value);