簡體   English   中英

測試不起作用,因為我的變量總是空的

[英]Test doesn't work, because my variable is always empty

我不知道為什么這個測試失敗了。 我創建了一個新的 function,手動測試它,它工作正常。 之后,我嘗試創建測試,但總是失敗。 我不知道為什么。 它只是應該清除數據庫中超過 1,5 年的所有記錄,但變量 historyToDelete 總是有 0 條記錄。 有完整的測試:

    using Microsoft.EntityFrameworkCore;
    using NUnit.Framework;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using TeamsAllocationManager.Contracts.EmployeeWorkingTypeHistory;
    using TeamsAllocationManager.Database;
    using TeamsAllocationManager.Domain.Models;
    using TeamsAllocationManager.Infrastructure.Handlers.EmployeeWorkingHistory;
    
    namespace TeamsAllocationManager.Tests.Handlers.EmployeeWorkingHistory
    {
        [TestFixture]
        public class ClearOldEmployeeWorkingTypeHistoryRecordsHandlerTest
        {
            private readonly ApplicationDbContext _context;
            public ClearOldEmployeeWorkingTypeHistoryRecordsHandlerTest()
            {
                DbContextOptions<ApplicationDbContext> options = new DbContextOptionsBuilder<ApplicationDbContext>()
                    .UseInMemoryDatabase(databaseName: GetType().Name)
                    .Options;
                _context = new ApplicationDbContext(options);
            }
    
            [SetUp]
            public void SetupBeforeEachTest()
            {
                _context.ClearDatabase();
    
                var employeeWorkingTypeHistory1 = new EmployeeWorkingTypeHistoryEntity 
                { 
                    EmployeeId = Guid.Parse("d6951ec1-c865-41bb-8b83-0fcd81745579"),
                    WorkspaceType = 0,
                    Created = new DateTime(2000, 01, 01)};
                var employeeWorkingTypeHistory2 = new EmployeeWorkingTypeHistoryEntity
                {
                    EmployeeId = Guid.Parse("8a6c4e1c-2c6d-4b70-a507-7bdae5f75429"),
                    WorkspaceType = 0,
                    Created = DateTime.Now
                };
    
                _context.EmployeeWorkingTypeHistory.Add(employeeWorkingTypeHistory1);
                _context.EmployeeWorkingTypeHistory.Add(employeeWorkingTypeHistory2);
    
                _context.SaveChanges();
            }
    
            [Test]
            public async Task ShouldClearHistory()
            {
                // given
                int numberOfHistoryToClear = 1;
                int expectedInDatabase = _context.EmployeeWorkingTypeHistory.Count() - numberOfHistoryToClear;
                
                var command = new ClearOldEmployeeWorkingTypeHistoryRecordsCommand();
    
                var deletionDate = command.TodayDate.AddMonths(-18);
                var historyToDelete = await _context.EmployeeWorkingTypeHistory
                    .Where(ewth => deletionDate > ewth.Created)
                    .ToListAsync();
    
                var commandHandler = new ClearOldEmployeeWorkingTypeHistoryRecordsHandler(_context);
    
                // when
                bool result = await commandHandler.HandleAsync(command);
    
                // then
                Assert.IsTrue(result);
                Assert.AreEqual(expectedInDatabase, _context.EmployeeWorkingTypeHistory.Count());
                //Assert.IsFalse(_context.EmployeeWorkingTypeHistory.Any(ewth => historyToDelete.Contains(ewth.Id)));
            }
        }
        
    }

如果我發現它失敗的原因,我將修復整個測試,但現在我被卡住了。

#更新 1

我發現了一個問題。 當我在 SetupBeforeEachTest 中創建 dbContext 時,我將 Created 設置為 2000.01.01。 一切正常,但是當我從這個開始到第一次測試時,當我檢查數據庫時,我總是有當前日期,SetupBeforeEach (2021.12.27) 中沒有提供

SaveChanges 在創建記錄時更新創建日期,因此如果您想更改創建日期以進行測試,您需要先創建新記錄,然后保存更改,更新並再次保存更改。

嘗試在您的 ShouldClearHistory() 方法中調用 SetupBeforeEachTest() 方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM