开发者

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 开发者_如何学Pythonuse 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.


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");
        ...
    }
}


Personally I'd do functional testing of the repositories against an actual database (possibly SQL Express). You could run those tests in CI only once a day.

All the unit tests for other classes can safely assume that the repositories work and use mock repositories.

EDIT: The above presumes that your repositories are solely used for data access; they basically just use LINQ or HQL. Keep the business logic out of them!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜