Base solution for your next web application
Open Closed

Understanding the difference between tests. #3956


User avatar
0
codenewa created

Hi,

I'm looking at the unit test: AuditLogAppService_Test. Should_Get_Audit_Logs()

Here, we setup the Arrange part by Adding some values to the AuditLogs table and then calling the service in the act step.

I followed the same step to test my own app service. However, I got the DbContext disposed exception. I followed through other discussions here and was able to get the tests to work by starting a unit of work.

How is it working in the AuditLogAppService_Test?


4 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    Can you show your test case?

  • User Avatar
    0
    codenewa created
    public class TenantCustmozation_Tests : AppTestBase
        {
            private readonly TenantCustomizationAppService _app;
    
            public TenantCustmozation_Tests()
            {
                _app = Resolve<TenantCustomizationAppService>();
            }
         
            [Fact]
            public async Task App_returns_tenantCustomization_if_present()
            {
                LoginAsDefaultTenantAdmin();
    
                var uowManager = Resolve<IUnitOfWorkManager>();
    
                using (uowManager.Begin())
                {
                    UsingDbContext(
                        ctx =>
                        {
                            ctx.Customizations.Add(new PatientPortal.POS.TenantCustomization
                            {
                                LoginBodyText = "Hello",
                                TenantId = AbpSession.TenantId.Value
                            });
    
    
                        });
    
                    var result = await _app.GetCustomizations();
    
                    result.ShouldNotBeNull();
    
                    result.LoginBodyText.ShouldBe("Hello");
                }
            }
    

    This is the working test. When I remove the uowManager.Begin(), the test fails because the DbContext has already been disposed when it reaches the _app.GetCustomizations() call.

  • User Avatar
    0
    aaron created
    Support Team

    Can you try resolving the interface ITenantCustomizationAppService instead of the class?

  • User Avatar
    0
    codenewa created

    Yeah. that was the case . :( Thank you for that.