Ends in:
7 DAYS
11 HRS
31 MIN
42 SEC
Ends in:
7 D
11 H
31 M
42 S
Open Closed

Testing Overridden AppService #1077


User avatar
0
raif created

https://docs.abp.io/en/abp/latest/Testing#example-testing-an-application-service

  • ABP Framework version: v4.2.2

  • UI type: MVC

  • DB provider: EF Core

  • Tiered (MVC) or Identity Server Separated (Angular): yes

  • Exception message and stack trace:

  • Autofac.Core.DependencyResolutionException: 'An exception was thrown while activating Siemens.NMM.Saas.Host.NMMTenantAppService.'

  • -InnerException {"None of the constructors found with 'Volo.Abp.Autofac.AbpAutofacConstructorFinder' on type 'Siemens.NMM.Saas.Host.NMMTenantAppService' can be invoked with the available services and parameters:\r\nCannot resolve parameter 'Microsoft.AspNetCore.Hosting.IWebHostEnvironment hosting' of constructor 'Void .ctor(Volo.Saas.Editions.IEditionRepository, Volo.Saas.Tenants.ITenantRepository, Volo.Saas.Tenants.ITenantManager, Volo.Abp.Data.IDataSeeder, Microsoft.Extensions.Configuration.IConfiguration, Microsoft.AspNetCore.Hosting.IWebHostEnvironment)'."} System.Exception {Autofac.Core.DependencyResolutionException}

  • Steps to reproduce the issue: I just override TenantAppService which is coming from Saas Module. Changes are very simple, one overriding of existing method, one totally new method

Now i would like to add tests related with those 2 method therefore i created Service Tests which inherits from Application Test Base

GetRequiredService<INMMTenantAppService>(); not able to resolve service.

What am i missing ?


2 Answer(s)
  • User Avatar
    0
    alper created
    Support Team Director

    Be aware that Unit Test module will not initialize when you are using a real IWebHostEnvironment . And you have a IWebHostEnvironment argument in the constructor of the ApplicationService. You need to mock WebHostEnvironment in order to get it run. Because there's no real WebHostEnvironment in the test phase:


    Mock class for IWebHostEnvironment:

    WebHostEnvironmentMock.cs

    public class WebHostEnvironmentMock : IWebHostEnvironment
        {
            public string EnvironmentName { get; set; }
            public string ApplicationName { get; set; }
            public string ContentRootPath { get; set; }
            public IFileProvider ContentRootFileProvider { get; set; }
            public IFileProvider WebRootFileProvider { get; set; }
            public string WebRootPath { get; set; }
    
            public WebHostEnvironmentMock()
            {
                var absoluteRootPath = Path.GetFullPath(string.Format("..{0}..{0}..{0}..{0}..{0}src{0}Siemens.NMM.Web, System.IO.Path.DirectorySeparatorChar));
                ContentRootPath = absoluteRootPath; //absolute path to your Web project
                WebRootPath = Path.Combine(absoluteRootPath, "wwwroot"); //this should point to wwwroot of your Web project
            }
        }
    
    

    Replace the implementation of IWebHostEnvironment to your mock class:

    NMMApplicationTestModule.cs

    public class NMMWebTestModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();
            context.Services.AddSingleton<IWebHostEnvironment>(new WebHostEnvironmentMock());
        }
    }
    
  • User Avatar
    0
    raif created

    Thx Alper. Somehow i thought that i override TenantAppService as it is. I forgot webhost and configuration injections..

Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06