Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Not able to debug any test #4375


User avatar
0
manojreddy created

Hi,

I'm not able to debug test, Run Test is working fine, But Debug Test is existing with the following error.

My projects were targeting to .net standard 1.0, now I am targeting to .net standard 2.0 with the same code.

Starting: MyCompany.MyProject.Tests [12/20/2017 6:57:51 AM Error] The active test run was aborted. Reason: Unable to communicate with test host process.


51 Answer(s)
  • User Avatar
    0
    manojreddy created

    Hi, I have found out the root cause of this issue.

    Actually, I'm trying to read two appsettings.json(one is named as appsettings.json and other is named as testingappsettings.json) in my Test Module and its reading only one at a time. So, my question is how to read from two appsettings.json file. I cannot use only one, I must use two JSON files.

    Please find my code below.

    using System;
    using Abp.AutoMapper;
    using Abp.Configuration.Startup;
    using Abp.Dependency;
    using Abp.Domain.Repositories;
    using Abp.Modules;
    using Abp.Net.Mail;
    using Abp.TestBase;
    using Abp.Zero.Configuration;
    using Castle.MicroKernel.Registration;
    using MyCompany.MyProject.Articles;
    using MyCompany.MyProject.Business.DAL;
    using MyCompany.MyProject.Business.Dto.Articles;
    using MyCompany.MyProject.Business.Dto.Classifications;
    using MyCompany.MyProject.Business.Dto.Common;
    using MyCompany.MyProject.Business.Dto.Currency;
    using MyCompany.MyProject.Business.Dto.Deposit;
    using MyCompany.MyProject.Business.Dto.PromoPack;
    using MyCompany.MyProject.Business.Dto.Property;
    using MyCompany.MyProject.Business.Dto.Suppliers;
    using MyCompany.MyProject.Business.Dto.Taxes;
    using MyCompany.MyProject.Business.Dto.TenderTypes;
    using MyCompany.MyProject.Business.Model.Articles;
    using MyCompany.MyProject.Business.Model.Common;
    using MyCompany.MyProject.Business.Model.Stores;
    using MyCompany.MyProject.Business.Model.Suppliers;
    using MyCompany.MyProject.Business.Model.Taxes;
    using MyCompany.MyProject.Business.Repositories.Interfaces;
    using MyCompany.MyProject.Business.Repositories.Repositories;
    using MyCompany.MyProject.Common;
    using MyCompany.MyProject.EntityFrameworkCore;
    using MyCompany.MyProject.PromoPack;
    using MyCompany.MyProject.Security.Recaptcha;
    using MyCompany.MyProject.Tests.DependencyInjection;
    using MyCompany.MyProject.Tests.Url;
    using MyCompany.MyProject.Tests.Web;
    using MyCompany.MyProject.Url;
    using NSubstitute;
    using MyCompany.MyProject.Business.Dto.Stores;
    using MyCompany.MyProject.Business.Services.Articles;
    using MyCompany.MyProject.Configuration;
    using MyCompany.MyProject.Processors.Core;
    using Abp.AspNetZeroCore;
    using Microsoft.Extensions.Configuration;
    using System.IO;
    
    namespace MyCompany.MyProject.Tests
    {
        [DependsOn(
            typeof(MyProjectServicesModule),
            typeof(MyProjectEntityFrameworkCoreModule),
            typeof(MyProjectRepositoryModule),
            typeof(AbpTestBaseModule))]
        public class MyProjectTestModule : AbpModule
        {
            public MyProjectTestModule(MyProjectEntityFrameworkCoreModule abpZeroTemplateEntityFrameworkCoreModule)
            {
                abpZeroTemplateEntityFrameworkCoreModule.SkipDbContextRegistration = true;
            }
    
            public override void PreInitialize()
            {
                var configuration = GetConfiguration();
    
                Configuration.UnitOfWork.Timeout = TimeSpan.FromMinutes(30);
                Configuration.UnitOfWork.IsTransactional = false;
    
                //Disable static mapper usage since it breaks unit tests (see https://github.com/aspnetboilerplate/aspnetboilerplate/issues/2052)
                Configuration.Modules.AbpAutoMapper().UseStaticMapper = false;
    
                //Use database for language management
                Configuration.Modules.Zero().LanguageManagement.EnableDbLocalization();
    
                RegisterFakeService<AbpZeroDbMigrator>();
    
                IocManager.Register<IAppUrlService, FakeAppUrlService>();
                IocManager.Register<IWebUrlService, FakeWebUrlService>();
                IocManager.Register<IRecaptchaValidator, FakeRecaptchaValidator>();
                IocManager.Register<IJobLeaseManager, BlobStorageJobLeaseManager>();
                IocManager.Register<IJobQueueManager, JobQueueManager>();
    
                Configuration.ReplaceService<IAppConfigurationAccessor, TestAppConfigurationAccessor>();
                Configuration.ReplaceService<IEmailSender, NullEmailSender>(DependencyLifeStyle.Transient);
    
                Configuration.Modules.AspNetZero().LicenseCode = configuration["AbpZeroLicenseCode"];
    
            }
    
            public override void Initialize()
            {
                ServiceCollectionRegistrar.Register(IocManager);
            }
    
            private void RegisterFakeService<TService>()
                where TService : class
            {
                IocManager.IocContainer.Register(
                    Component.For<TService>()
                        .UsingFactoryMethod(() => Substitute.For<TService>())
                        .LifestyleSingleton()
                );
            }
    
            private static IConfigurationRoot GetConfiguration()
            {
                return AppConfigurations.Get(Directory.GetCurrentDirectory(), addUserSecrets: true);
            }
        }
    }
    

    I have overwritten the TestAppConfigurationAccessor file like this.

    using Abp.Dependency;
    using Microsoft.Extensions.Configuration;
    using MyCompany.MyProject.Configuration;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    
    namespace MyCompany.MyProject.Tests
    {
        public class TestAppConfigurationAccessor : IAppConfigurationAccessor, ISingletonDependency
        {
            public IConfigurationRoot Configuration { get; }
    
            public TestAppConfigurationAccessor()
            {
                Configuration = BuildConfig();
            }
    
            private IConfigurationRoot BuildConfig()
            {
                var currentPath = Directory.GetCurrentDirectory();
    
                var config = new ConfigurationBuilder()
                    .AddJsonFile(Path.Combine(currentPath, "..", "..", "..", "..", "..", "src", "MyCompany.MyProject.Web.Host", $"appsettings.json"), false);
    
                config.AddJsonFile(Path.Combine(currentPath, $"testingappsettings.json"), false);
    
                return config.Build();
            }
        }
    }
    

    AppConfigurations file:

    using System.Collections.Concurrent;
    using Abp.Extensions;
    using Abp.Reflection.Extensions;
    using Microsoft.Extensions.Configuration;
    
    namespace MyCompany.MyProject.Configuration
    {
        public static class AppConfigurations
        {
            private static readonly ConcurrentDictionary<string, IConfigurationRoot> ConfigurationCache;
    
            static AppConfigurations()
            {
                ConfigurationCache = new ConcurrentDictionary<string, IConfigurationRoot>();
            }
    
            public static IConfigurationRoot Get(string path, string environmentName = null, bool addUserSecrets = false)
            {
                var cacheKey = path + "#" + environmentName + "#" + addUserSecrets;
                return ConfigurationCache.GetOrAdd(
                    cacheKey,
                    _ => BuildConfiguration(path, environmentName, addUserSecrets)
                );
            }
    
            private static IConfigurationRoot BuildConfiguration(string path, string environmentName = null, bool addUserSecrets = false)
            {
                var builder = new ConfigurationBuilder()
                    .SetBasePath(path)
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
    
                if (!environmentName.IsNullOrWhiteSpace())
                {
                    builder = builder.AddJsonFile($"appsettings.{environmentName}.json", optional: true);
                }
    
                builder = builder.AddEnvironmentVariables();
    
                if (addUserSecrets)
                {
                    builder.AddUserSecrets(typeof(AppConfigurations).GetAssembly());
                }
    
                return builder.Build();
            }
        }
    }
    

    testingappsettings.json:

    {
      "MasterDataFiles": {
        "TestingEntity1": {
          "UseStaging": "true"
        },
        "TestingEntity2": {
          "UseStaging": "false"
        }
      }
    }
    

    appsettings.json:

    {
    "AbpZeroLicenseCode": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    }
    
  • User Avatar
    0
    aaron created
    Support Team

    Which folder is testingappsettings.json located in?

  • User Avatar
    0
    manojreddy created

    In the same testproject folder

  • User Avatar
    0
    aaron created
    Support Team

    Which file is not read?

  • User Avatar
    0
    manojreddy created

    I think appsettings.json, because in the following line >configuration["AbpZeroLicenseCode"] returns null.

    Configuration.Modules.AspNetZero().LicenseCode = configuration["AbpZeroLicenseCode"];
    
  • User Avatar
    0
    aaron created
    Support Team

    Doesn't your Test project have an appsettings.json with the license code?

  • User Avatar
    0
    manojreddy created

    yes, Test Project has appsettings.json has the license code.

    Did you check my post?, I have mentioned it in my reply.

    Please check the post first.

  • User Avatar
    0
    aaron created
    Support Team

    Then explain this line:

    .AddJsonFile(Path.Combine(currentPath, "..", "..", "..", "..", "..", "src", "MyCompany.MyProject.Web.Host", $"appsettings.json"), false);
    
  • User Avatar
    0
    manojreddy created

    I have kept appsettings.json file in Host project as well with the license code. And I'm trying to read it from that location.

  • User Avatar
    0
    aaron created
    Support Team

    In any case, don't construct paths like that. Try this:

    private IConfigurationRoot BuildConfig()
    {
        var currentPath = Directory.GetCurrentDirectory();
    
        var config = new ConfigurationBuilder()
            .SetBasePath(typeof(MyProjectWebHostModule).GetAssembly().GetDirectoryPathOrNull())
            .AddJsonFile("appsettings.json");
    
        config
            .SetBasePath(currentPath)
            .AddJsonFile("testingappsettings.json");
    
        return config.Build();
    }
    
  • User Avatar
    0
    manojreddy created

    I don't have MyProjectWebHostModule.cs file, shall I add new? Do I need to add any dependency in that?

  • User Avatar
    0
    aaron created
    Support Team

    It should be StanchionWebHostModule in your project.

  • User Avatar
    0
    manojreddy created

    Still facing the issue. Getting the below message while running test case.

    Its searching appsettings.json in Test Module path.

    [12/22/2017 7:32:04 AM Informational] [xUnit.net 00:01:13.1092474] Starting: MyCompany.MyProject.Tests [12/22/2017 7:32:09 AM Error] [xUnit.net 00:01:17.2516509] MyCompany.MyProject.Tests.MyEntity.MyEntityAppService_Test.Should_Create_MyEntity_With_Valid_Arguments [FAIL] [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2540945] Castle.MicroKernel.ComponentActivator.ComponentActivatorException : ComponentActivator: could not instantiate MyCompany.MyProject.Tests.TestAppConfigurationAccessor [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2541647] ---- System.IO.FileNotFoundException : The configuration file 'appsettings.json' was not found and is not optional. The physical path is 'C:\tt\MyProject\aspnet-core\test\MyCompany.MyProject.Tests\bin\Debug\netcoreapp2.0\appsettings.json'. [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2556024] Stack Trace: [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2567318] at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstanceCore(ConstructorCandidate constructor, Object[] arguments, Type implType) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2568189] at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstance(CreationContext context, ConstructorCandidate constructor, Object[] arguments) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2568730] at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2569150] at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context, Burden burden) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2569605] at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.CreateInstance(CreationContext context, Boolean trackedExternally) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2582037] at Castle.MicroKernel.Lifestyle.SingletonLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2582823] at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden& burden) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2583437] at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2583926] at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2584414] at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateConstructorArguments(ConstructorCandidate constructor, CreationContext context) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2584852] at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.Instantiate(CreationContext context) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2585230] at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2585655] at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context, Burden burden) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2586074] at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.CreateInstance(CreationContext context, Boolean trackedExternally) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2586457] at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2586923] at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden& burden) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2587307] at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2587745] at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments, IReleasePolicy policy) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2588209] at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2589080] D:\Github\aspnetboilerplate\src\Abp\Dependency\IocResolverExtensions.cs(30,0): at Abp.Dependency.IocResolverExtensions.ResolveAsDisposable[T](IIocResolver iocResolver, Type type) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2589677] D:\Github\aspnetboilerplate\src\Abp\Configuration\SettingDefinitionManager.cs(32,0): at Abp.Configuration.SettingDefinitionManager.Initialize() [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2590223] D:\Github\aspnetboilerplate\src\Abp\AbpKernelModule.cs(73,0): at Abp.AbpKernelModule.PostInitialize() [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2590653] at System.Collections.Generic.List1.ForEach(Action1 action) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2591088] D:\Github\aspnetboilerplate\src\Abp\AbpBootstrapper.cs(158,0): at Abp.AbpBootstrapper.Initialize() [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2591515] D:\Github\aspnetboilerplate\src\Abp.TestBase\TestBase\AbpIntegratedTestBase.cs(53,0): at Abp.TestBase.AbpIntegratedTestBase1.InitializeAbp() [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2592108] C:\tt\MyProject\aspnet-core\test\MyCompany.MyProject.Tests\AppTestBase.cs(27,0): at MyCompany.MyProject.Tests.AppTestBase..ctor() [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2592664] C:\tt\MyProject\aspnet-core\test\MyCompany.MyProject.Tests\MyEntity\MyEntityAppServiceTestBase.cs(10,0): at MyCompany.MyProject.Tests.MyEntity.MyEntityAppServiceTestBase..ctor() [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2593184] C:\tt\MyProject\aspnet-core\test\MyCompany.MyProject.Tests\MyEntity\MyEntityappservice_test.cs(12,0): at MyCompany.MyProject.Tests.MyEntity.MyEntityAppService_Test..ctor() [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2593581] ----- Inner Stack Trace ----- [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2593969] at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2594422] at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load() [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2594790] at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList1 providers) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2595191] at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build() [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2595649] C:\tt\MyProject\aspnet-core\test\MyCompany.MyProject.Tests\TestAppConfigurationAccessor.cs(34,0): at MyCompany.MyProject.Tests.TestAppConfigurationAccessor.BuildConfig() [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2596233] C:\tt\MyProject\aspnet-core\test\MyCompany.MyProject.Tests\TestAppConfigurationAccessor.cs(19,0): at MyCompany.MyProject.Tests.TestAppConfigurationAccessor..ctor() [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2596609] at lambda_method(Closure , Object[] ) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2597049] at Castle.Core.Internal.ReflectionUtil.Instantiate[TBase](Type subtypeofTBase, Object[] ctorArgs) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2597502] at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstanceCore(ConstructorCandidate constructor, Object[] arguments, Type implType) [12/22/2017 7:32:09 AM Informational] [xUnit.net 00:01:17.2980342] Finished: MyCompany.MyProject.Tests [12/22/2017 7:32:09 AM Informational] ========== Run test finished: 1 run (0:01:32.5026945) ==========

  • User Avatar
    0
    aaron created
    Support Team

    So only the last SetBasePath matters, as it's only used on Build. Why don't you just use the appsettings.json in Test project?

  • User Avatar
    0
    manojreddy created

    Didn't get >So only the last SetBasePath matters, as it's only used on Build. , Could you please explain.

  • User Avatar
    0
    manojreddy created

    @aaron any updates?

  • User Avatar
    0
    aaron created
    Support Team

    Why don't you just use the appsettings.json in Test project?

  • User Avatar
    0
    manojreddy created

    I used appsettings in test project also, it’s not working too.

    You can check the same in the code shared with you.

  • User Avatar
    0
    manojreddy created

    Any updates?

  • User Avatar
    0
    aaron created
    Support Team

    Your original way works. configuration["AbpZeroLicenseCode"] returns null because:

    • You did configuration = GetConfiguration(); at the start of the method, way before you replaced IAppConfigurationAccessor.
    • The static method GetConfiguration() doesn't use IAppConfigurationAccessor. It's just a helper to get environment variables.
  • User Avatar
    0
    manojreddy created

    So, Could you help me with the working code? Thanks

  • User Avatar
    0
    aaron created
    Support Team

    Your original way:

    .AddJsonFile(Path.Combine(currentPath, "..", "..", "..", "..", "..", "src", "MyCompany.MyProject.Web.Host", $"appsettings.json"), false);
    

    Get configuration:

    IocManager.Register<IAppConfigurationAccessor, TestAppConfigurationAccessor>();
    var configuration = IocManager.Resolve<IAppConfigurationAccessor>().Configuration;
    Configuration.Modules.AspNetZero().LicenseCode = configuration["AbpZeroLicenseCode"];
    
  • User Avatar
    0
    manojreddy created

    and what about testingappsettings.json?

    I want to use/read both json file. appsettings.json and testingappsettings.json

  • User Avatar
    0
    aaron created
    Support Team

    Your original way works. Your original way:

    #4375

  • User Avatar
    0
    aaron created
    Support Team

    If you can put both appsettings.json and testappsettings.json in the same folder, just follow these 3 steps:

    • Add the line below in AppConfigurations:
    private static IConfigurationRoot BuildConfiguration(string path, string environmentName = null, bool addUserSecrets = false)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(path)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
            .AddJsonFile("testappsettings.json", optional: true, reloadOnChange: true); // This line
    
        // ...
    }
    
    • Change this method in Helpers.cs:
    public static IConfigurationRoot GetConfiguration()
    {
        return SingletonDependency<IAppConfigurationAccessor>.Instance.Configuration; // This line
    }