In my job we are writing web services, which are called by an app. We are working in agile mind set using domain driven design. As in DDD we have domain and application layer. However, we encountered problem in writing unit tests for those layers, because it seems that we are testing domain logic twice: in domain unit tests and again in application unit tests:
Application unit test
[TestMethod]
public void UserApplicationService_SignOut_ForExistingUserWithBalance_ShouldClearBalanceAndSignOut()
{
//Arrange
long merchantId = 1;
long userId = 1;
var transactionId = "001";
var id = "122";
var user = Help.SetId<User>(User.Register(id, new DateTime(2015, 01, 01, 00, 00, 01)), userId);
_usersDb.Add(user);
var userBonusBalanceRepository = _testContext.MoqUnitOfWork.MockUnitOfWork.Object.GetUserBonusAccountRepository();
UserBonusAccount uba = userBonusBalanceRepository.GetForUser(user);
uba.PayTo(
new Domain.Core.Money { TotalAmount = 10, BonusAmount = 0 },
new Domain.Core.Outlet
{
BonusPercentage = 50,
IsLoyalty = true,
Id = id,
OutletId = "111"
},
transactionId,
DateTime.Now);
userBonusBalanceRepository.Update(uba);
//Act
_testContext.UserApplicationService.SignOut(id);
//Assert
var firstOrDefault = this._balances.FirstOrDefault(x => x.UserId == user.Id && x.MerchantId == merchantId);
Assert.IsTrue(firstOrDefault != null && firstOrDefault.Balance == 0);
Assert.IsNotNull(this._transactions.Where(x => x.Id == transactionId && x.Type == BonusTransactionType.EraseFunds));
}
Domain unit test
[TestMethod]
public void UserBonusAccount_ClearBalances_shouldClearBalancesForAllMerchants()
{
long userId = 1;
long firstMerchantId = 1;
long secondMerchantId = 2;
User user = User.Register("111", new DateTime(2015, 01, 01, 00, 00, 01));
Shared.Help.SetId(user, userId);
List<BonusTransaction> transactions = new List<BonusTransaction>();
List<BonusBalance> balances = new List<BonusBalance>();
var userBonusAccount = UserBonusAccount.Load(transactions.AsQueryable(), balances.AsQueryable(), user);
userBonusAccount.PayTo(new Money {TotalAmount = 100, BonusAmount = 0},
new Outlet
{
BonusPercentage = 10,
IsLoyalty = true,
MerchantId = firstMerchantId,
OutletId = "4512345678"
}, "001", DateTime.Now);
userBonusAccount.PayTo(new Money {TotalAmount = 200, BonusAmount = 0},
new Outlet
{
BonusPercentage = 10,
IsLoyalty = true,
MerchantId = secondMerchantId,
OutletId = "4512345679"
}, "002", DateTime.Now);
userBonusAccount.ClearBalances();
Assert.IsTrue(userBonusAccount.GetBalanceAt(firstMerchantId) == 0);
Assert.IsTrue(userBonusAccount.GetBalanceAt(secondMerchantId) == 0);
}
As you can see these both tests checks whether user balance is 0, which is domain responsibility. Thus the question is: how application layer unit tests should look like and what it should test? Somewhere I read that unit tests should test in "application services for flow control and domain models for business rules". Could someone elaborate more and give some examples what application layer unit tests should test and look like?