Ends in:
2 DAYS
11 HRS
28 MIN
23 SEC
Ends in:
2 D
11 H
28 M
23 S
Open Closed

CurrentTenant is returning NullReferenceException in a Test method #1647


User avatar
0
agilmore created

When I run the Should_CopyTemplate_Test() test case below, the reference to CurrentTenant in the method it calls is returning NullReferenceException, as if the ICurrentTenant is not being injected.

I have this test set up:

    {

        private ISltnRepository _sltnRepository;
        private ICurrentTenant _currentTenant;

        public SltnManager_Tests()
        {
            _sltnRepository = GetRequiredService<ISltnRepository>();
            _currentTenant = GetRequiredService<ICurrentTenant>();
        }

        [Fact]
        public async Task Should_CopyTemplate_Test()
        {
            // Arrange
            var sltnManager = new SltnManager(_sltnRepository);

            // Act
            Guid sltnId = Guid.Parse("00000000-14ba-4f0f-8417-c2f312768aea");
            var sltn = await sltnManager.CopyTemplate(sltnId);

            //Assert
            sltn.Name.ShouldBe("");
        }
    }

Testing this method:

        public async Task<Sltn> CopyTemplate(Guid templateId)
     {
         // Get the template solution from the host tenant
         Sltn template;
         using (CurrentTenant.Change(null))
         {
             template = await _sltnRepository.GetAsync(templateId);
         }
         // set a branches list from the template
         List<Branch> branches = new List<Branch>();
         foreach(var templateBranch in template.Branches)
         {
             branches.Add(new Branch(GuidGenerator.Create(), templateBranch.Name, templateBranch.Description, templateBranch.AppUserId, templateBranch.ParentBranchId));
         }
         
         //create a new solution for the tenant from the template
         var sltn = new Sltn(GuidGenerator.Create(), template.Name, template.Description, template.DisplayOrder, branches);
         await _sltnRepository.InsertAsync(sltn);
         return sltn;
     }

However CurrentTenant.Change is returning NullReferenceException on CurrentTenant.


8 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    // Arrange var sltnManager = new SltnManager(_sltnRepository);

    Why not get service through DI container? eg: GetRequiredService<SltnManager>()

  • User Avatar
    0
    agilmore created

    Hi.

    Thanks, that fixed the issue.

    A supplementary question, does the SQlite EF Core Context used by the test framework support the IncludeDetails=true.

    In my code above foreach(var templateBranch in template.Branches), Branches returns null.

    Branches are added in a seeding class, which worked for SQL Server.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Have you configured the Includeextension in your EF Core module?

    https://github.com/abpframework/abp/blob/dev/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityEfCoreQueryableExtensions.cs#L8 https://github.com/abpframework/abp/blob/dev/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs#L281

  • User Avatar
    0
    agilmore created

    I haven't.

    The Abp framework EF documentations says GetAsync will include details as default. Does that not work for Sqlite? Or does it have to be configuredevery time? That would be a good addition to your documentation.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Does that not work for Sqlite?

    Yes, Can you try same way just like the Identity module?

  • User Avatar
    0
    agilmore created

    Ok.

    I made the change and it worked.

    Just to clarify.

    1. I only need to add the EfCoreQuerableExtensions because EF is using SQLite in Test mode ?
    2. Or, EfCoreQuerableExtensions need to be configured for SQL Server in Production as well ?

    Thanks - Adam

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You always need to use EfCoreQueryableExtensions, This is recommended.

    It's related to EF Core, will be works for any provider, eg: sql server, sqlite,mysql.

  • User Avatar
    0
    agilmore created

    Ok, Thanks very much for your help.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 02, 2024, 12:35