Tests failing only when running on Rider

Hello.
I have some unit tests on my solution that if I run them with `dotnet test`, or if I use VisualStudio 2022, they run successfully, but if I run them on Rider, they fail with an exception.

These tests in particular will use the following code snippet to get an instance of the program, and that is where it is throwing the exception:

            var server = Host.CreateDefaultBuilder()
                .ConfigureWebHostDefaults(options => {
                    options.UseStartup<Project.Startup>();
                })
                .Build();

The tests are failing apparently because they are not being able to correctly activate all dependencies from that application. This is the exception being logged:

System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Domain.Logging.IQueueLoggingDataStore Lifetime: Scoped ImplementationType: Domain.Logging.AzureQueueLoggingDataStore': Unable to resolve service for type 'System.String' while attempting to activate 'Domain.Logging.AzureQueueLoggingDataStore'.) (Error while validating the service descriptor 'ServiceType: Domain.IMailDataStore Lifetime: Scoped ImplementationType: Domain.MailDataStore': Unable to resolve service for type 'Domain.ConfigDataStore.Model.ReadOnlyLoggingContext' while attempting to activate 'Domain.MailDataStore'.) ---> System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: Domain.Logging.IQueueLoggingDataStore Lifetime: Scoped ImplementationType: Domain.Logging.AzureQueueLoggingDataStore': Unable to resolve service for type 'System.String' while attempting to activate 'Domain.Logging.AzureQueueLoggingDataStore'. ---> System.InvalidOperationException: Unable to resolve service for type 'System.String' while attempting to activate 'Domain.Logging.AzureQueueLoggingDataStore'.
    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(ServiceDescriptor serviceDescriptor, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ValidateService(ServiceDescriptor descriptor)
--- End of inner exception stack trace ---
    at Microsoft.Extensions.DependencyInjection.ServiceProvider.ValidateService(ServiceDescriptor descriptor)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
--- End of inner exception stack trace ---
    at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
   at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder)
   at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter`1.CreateServiceProvider(Object containerBuilder)
   at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   at UnitTests.StartupTests.StartupTest() in

That AzureQueueLoggingDataStore class has a constructor that gets a connection string as a parameter, and that is what seems to be the issue. 

But again, those tests run fine outside of Rider, and this is production code, they run the tests on deployment, and are running fine on the servers. It seems to be something on how Rider is running them. There is also another similar test that uses the same code snippet, but do not have that data store as a dependency, and that one runs fine everywhere.


Is there any way to get more details on the tests execution to maybe compare to see what´s going on?

I'm using the latest 2024.1 Rider version (although I've tested it on the last 2023.3.4 version and the error is the same), and it's a .NET6 project with MSTest running on Windows 10.

1
5 comments

May I know if the exceptions are from the Rider Unit Test tool or other components? It could be easier to understand the symptom you are seeing if you can provide a minimal reproduction in a sample project.

Also, it's worth checking if the Test Runner and Test Settings of the project are correctly loaded in Settings | Build, Execution, Deployment | Unit Testing | MSTest.

Please upload the file to our server and share the ID.

0

Hi. Thanks for the reply. I've managed to create a sample project that reproduces the issue (it happened on two different Windows environments. I've not tests on other OS).

The project has two test cases. Both will run successfully when executed using the dotnet test command or from VisualStudio, but one of them will fail when the tests are run on Rider.

I've looked through my MS Test settings, but nothing popped into view that would be causing the issue; I'm not sure what to look for.

And the exception is from the code itself. It seems it's being unable to build the service on runtime when the test is executed from Rider, and that does not happen when running from the dotnet cli or visualstudio.

Upload id: 2024_04_15_ihYpAi5badP7gZxP51ff7y (files: dotnet_test_result.png and 3 more)

0

I have same issue. I have hundreds of tests (using DI) that are passing in Visual Studio 2022 and earlier versions of Rider (2023.2) but now fail in Rider 2024.1

0

actually, it appears that scoped services used to work by calling them in a test without scope. The below code shows how the testUsedToWork was fetched successfully. I now have to CreateScope first in order to get tests to work

 

public class Test
{
}
[Fact]
public void ShowRiderError(){
    var webApplicationBuilder = WebApplication.CreateBuilder();

    webApplicationBuilder.Services.AddScoped<Test>();

    var webApplication = webApplicationBuilder.Build();

    var scope  = webApplication.Services.CreateScope();
    var getTestResult = scope.ServiceProvider.GetRequiredService<Test>();
 

//this line will now fail
    var testUsedToWork = webApplication.Services.GetRequiredService<Test>();

}

0

LFrigoDeSouza The problem is caused by the default DOTNET_ENVIRONMENT environment variable added in Test Runner.

We have a corresponding YouTrack ticket here:

RSRP-495598 XUnit Test with Dependency Injection fails after Rider Update

Please refer to the comment in the ticket: delete the environment variable in Test Runner settings, or disable the environment variable validation explicitly in your test.

Richloveswork Please also have a check if the workaround is working for you.

0

Please sign in to leave a comment.