如何测试我的存储库实施?

发布于 2024-10-18 05:22:20 字数 265 浏览 4 评论 0原文

我使用 NUnit 作为测试单元。我在域上有我的接口,因此我准备在持久层中实现这些接口。我的问题是,您实际上如何进行单元测试来测试这些存储库?我认为直接从数据库进行测试并不是一个好主意。我听说有人正在使用 SQLite,但使用模拟也可以吗?当你可以提供带有实际实体的模拟时,为什么人们使用 SQLite 作为内存数据库?

任何例子也将受到欢迎。

注意:这是用 C# 编码的存储库,将使用 NHibernate 和 Fluent NHibernate 作为映射。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心病无药医 2024-10-25 05:22:20

这当然取决于,但在大多数情况下,我想说,通常只需在测试中模拟存储库并使用内存中的 SQLite 数据库来测试您的映射就足够了(FluentNHibernate 持久性规范测试)。

对于使用 SQLite 的 NUnit 映射测试,我使用以下基类:

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
}

从上述基类派生的示例映射测试类可能如下所示:

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

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");
        ...
    }
}
苍风燃霜 2024-10-25 05:22:20

就我个人而言,我会针对实际数据库(可能是 SQL Express)对存储库进行功能测试。您每天只能在 CI 中运行一次这些测试。

其他类的所有单元测试都可以安全地假设存储库正常工作并使用模拟存储库。

编辑:以上假设您的存储库仅用于数据访问;他们基本上只使用 LINQ 或 HQL。让他们远离业务逻辑!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文