Search code examples
unit-testingnunitddd-repositories

How to test my repositories implementation?


I am using NUnit for test units. I have my interface on the domain so i am ready to make implementation of those interfaces in the persistence layer. My question is how do you actually make the unit tests for testing those repositories ? i believe this isnt a good idea to test directly from the database. Ive heard poeple that are using SQLite but it is okay to use mocks instead ? why are the poeple using SQLite for in-memory database when you can provide a mock with actuals entities ?

Any example would be welcome too.

Note: This is intended to be repositories coded in C# that gonna use NHibernate and Fluent NHibernate as mapping.

Thanks.


Solution

  • It of course depends, but in most cases I'd say it's generally enough to just mock the repositories in your tests and using the in-memory SQLite database only to test your mappings (FluentNHibernate Persistence specification testing).

    For the NUnit mappings tests with SQLite I'm using the following base class:

    public abstract class MappingsTestBase
    {
        [SetUp]
        public void Setup()
        {
            _session = SessionFactory.OpenSession();
            BuildSchema(_session);
        }
    
        [TestFixtureTearDown]
        public void Terminate()
        {
            _session.Close();
            _session.Dispose();
    
            _session = null;
            _sessionFactory = null;
            _configuration = null;
        }
    
        #region NHibernate InMemory SQLite Session
    
        internal static ISession _session;
    
        private static ISessionFactory _sessionFactory;
        private static Configuration _configuration;
    
        private static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                {
                    FluentConfiguration configuration = Fluently.Configure()
                        .Database(SQLiteConfiguration.Standard.InMemory().ShowSql)
                        .Mappings(m => m.FluentMappings
                            .AddFromAssemblyOf<NHibernateSession>())
                        .ExposeConfiguration(c => _configuration = c);
    
                    _sessionFactory = configuration.BuildSessionFactory();
                }
    
                return _sessionFactory;
            }
        }
    
        private static void BuildSchema(ISession session)
        {
            SchemaExport export = new SchemaExport(_configuration);
            export.Execute(true, true, false, session.Connection, null);
        }
    
        #endregion
    }
    

    An example mappings test class deriving from the above base class could then look like the following:

    [TestFixture]
    public class MappingsTest : MappingsTestBase
    {
        [Test]
        public void Persistence_Employee_ShouldMapCorrectly()
        {
            Category employee = new PersistenceSpecification<Employee>(_session)
                .CheckProperty(e => e.Id, 1)
                .CheckProperty(e => e.FirstName, "John")
                .CheckProperty(e => e.LastName, "Doe")
                .VerifyTheMappings();
            ...
            Assert.Equals(employee.FirstName, "John");
            ...
        }
    }