Base solution for your next web application
Open Closed

Execute hard delete in unit test #9166


User avatar
0
FVDH created

Hello,

Is there a way to execute a hard delete on a mocked database in a unit test? In an appservice we can excecute IRepository.HardDelete().

We want to execute the hard delete in the unit test (class derived from AppTestBase), we can't resolve the IRepository there.

Following example explains what we are trying to do:

        [Fact]
        public async Task Should_Delete_Entity_With_LocalizedString()
        {
            // Create a new test entity
            await _testLocalizedResourceAppService.CreateTestEntityA();

            // Test for the create entity
            UsingDbContext(context =>
            {
                var resources = context.TestLocalizedResources.Include(x => x.Values).ToList();
                resources.Count.ShouldBe(1);
                resources.First().Values.Count.ShouldBe(2);
            });

            // Delete the created entity
            var entityAList = await _testLocalizedResourceAppService.GetList();
            await _testLocalizedResourceAppService.Delete(entityAList.Items.First().Id);

            // The entity is soft deleted, the resources still exists
            UsingDbContext(context =>
            {
                context.TestEntitiesA.Count().ShouldBe(1);
                context.TestEntitiesA.First().IsDeleted.ShouldBeTrue();

                var resources = context.TestLocalizedResources.Include(x => x.Values).ToList();
                resources.Count.ShouldBe(1);
                resources.First().Values.Count.ShouldBe(2);

            });

            // Execute hard delete
            // How to execute hard delete here?

            // The rows should be deleted from the database
            UsingDbContext(context =>
            {
                context.TestEntitiesA.Count().ShouldBe(0);

                context.LocalizedResources.Count().ShouldBe(0);
                context.LocalizedResourceValues.Count().ShouldBe(0);
            });
        }

We already tried variants of following code without success:

            var uowOptions = new UnitOfWorkOptions();
            uowOptions.FilterOverrides.Add(new DataFilterConfiguration(AbpDataFilters.SoftDelete, false));

            await WithUnitOfWorkAsync(() =>
            {
                return _testLocalizedResourceAppService.Delete(entityAList.Items.First().Id);
            }, uowOptions);

3 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    hi

    We want to execute the hard delete in the unit test (class derived from AppTestBase), we can't resolve the IRepository there.

    I think you should find a way to use IRepository because HardDelete is its extension method.

    You are already available using _testLocalizedResourceAppService Why is the repository not available?

    If you can provide a project to reproduce the problem, I can download and check it. : ) https://aspnetboilerplate.com/Templates

  • User Avatar
    0
    FVDH created

    Hi maliming,

    While we were working out a test project to reproduce the problem we found a solution for our problem.

    We were able to use IRepository.InsertAndGetId. The problem with the HardDelete method is that is didn't start a UnitOfWork.

    Following code in our test method works:

                // Act hard delete
                WithUnitOfWork(() =>
                {
                    _projectRepository.HardDelete(p => p.Id == projectId);
                });
    

    When we do not start a unit of work, we get a NullReference exception.

  • User Avatar
    0
    maliming created
    Support Team

    hi

    The HardDelete function requires a unit of work. : )

    https://github.com/aspnetboilerplate/aspnetboilerplate/pull/5655