Unit tests and database

This unit test question raised another thing that bothered me. I went back and forth in three ways to run unit tests when I hit the database.

  • Create mock objects and connect them. This has the advantage that you do not need a database, but it takes a lot of time, and I'm not sure how much return on investment I get. I get into IOC and moq a bit, but it still seems sick.
  • Create setup and break database scripts to create known cases and test them. Again, this can be time consuming, but still easier to create than mocking up objects most of the time. And other people at work can still work, believing that they have an SQL server on their local host.
  • Manually check the dev database and modify the unit tests. Intensively manual work, but if I have a “test suite” that doesn't change, it seems to work fine. On my car, at least :-).

I know that option 1 is the “right” way to do unit tests, but out of the three, maybe the option I used the least (although the last projects were with IOC, so the door is open for me). I understand that a lot depends on what exactly is being mocked and what is being tested, but what am I missing here?

If the context helps, I am in the C # store, creating my own applications, only a few developers.

+3
source share
4 answers

, , IQueryable<T>. , : List<T> AsQueryable. , moq .

, , (, ). , Moles , , . IOC, .

+3

( 1). .

( 2), ( 2). ? , ( O/R, ..), .

, ( .NET, NUnit SqlServer ):

using System.Transactions;
using NUnit.Framework;
namespace Crown.Util.TestUtil
{
    [TestFixture]
    public class PersistenceTestFixture
    {
        public TransactionScope TxScope { get; private set; }

        [SetUp]
        public void SetUp()
        {
            TxScope = new TransactionScope();
        }

        [TearDown]
        public void TearDown()
        {
            if (TxScope != null)
            {
                TxScope.Dispose();
                TxScope = null;
            }
        }
    }
}

.

+2

. , , , . API, , DAO, ?

. , , . , hsql, . , . , , , .

mocks , , . . -, mocks, . , , , , , .

, . , , - , . , 100% .

+1

, , ?

If you separate your business-level and service-level logic from your persistence code, you should easily isolate the code you want to use unit test without having a database.

One of the most important principles of unit testing is isolation and testing separately. When you have a clear idea of ​​how to do this, unit testing is easy. When you do not, unit testing becomes difficult.

0
source

Source: https://habr.com/ru/post/1751887/


All Articles