vendredi 29 janvier 2016

How to unit test NpgsqlDataReader extensions in C#

I've been playing around with some ADO.NET extensions inspired by a PostgreSQL Pluralsight article by Rob Conery.

I'm new to unit testing and have been trying to learn some more about it by writing some tests around a couple of extension methods I've written for the NpgsqlDataReader class.

An example of a method I'm trying to test:

    public static T ToEntity<T>(this NpgsqlDataReader reader) where T : new()
    {
        var result = new T();
        var properties = typeof(T).GetProperties();

        foreach (var prop in properties)
        {
            for (var i = 0; i < reader.FieldCount; i++)
            {
                if (reader.GetName(i).Replace("_", "").Equals(prop.Name, StringComparison.InvariantCultureIgnoreCase))
                {
                    var val = reader.GetValue(i) != DBNull.Value ? reader.GetValue(i) : null;
                    prop.SetValue(result, val);
                }
            }
        }

        return result;
    }

I'm not sure how to even start mocking the database connection, as all I want to test is functionality of iterating over the returned result object and mapping the columns to the properties of a generic entity class.

How would I go about testing the entity mapping, without accessing the database?

Thanks

Aucun commentaire:

Enregistrer un commentaire