question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

package seems to break functional testing with a Microsoft.AspNetCore.TestHost.Testserver

See original GitHub issue

I’m using Quartz like this in my code and everything works as expected:

services.AddQuartz(q =>
{
    q.SchedulerId = "CutOff-time-scheduler";
    
    q.UseMicrosoftDependencyInjectionScopedJobFactory(options =>
    {
        options.CreateScope = true;
        options.AllowDefaultConstructor = false;
    });

    q.ScheduleJob<SendEventIfMarketCutOffTimePassedJob>(trigger => trigger
        .WithIdentity("daily-trigger")
        .WithCronSchedule(configuration["CutOffTimeCronSchedule"])
        .WithDescription("triggers the 'SendEventIfMarketECutOffTimePassed' job every day")
        .StartNow()
    );
});

services.AddQuartzHostedService(options =>
{
    // when shutting down we want jobs to complete gracefully
    options.WaitForJobsToComplete = true;
});

At the same time there are a bunch of functional tests using an IClassFixture like this:

[Collection("Sequential")]
public class AssetFunctionalTest : IClassFixture<TestFixture>
{
    private readonly IMarketRepository _marketRepository;
    private readonly HttpClient _client;
    private const string AssetUrl = "/WENGRIN/MicroService-Asset/Asset";
    
    public AssetFunctionalTest(TestFixture fixture)
    {
        _client = fixture.Client;
        _marketRepository = fixture.MarketRepository;
    }

Where the TestFixture class creates a test server like this:

 protected TestFixture(string relativeTargetProjectParentDir)
{
    var startupAssembly = typeof(Startup).GetTypeInfo().Assembly;
    var contentRoot = GetProjectPath(relativeTargetProjectParentDir, startupAssembly);

    var configurationBuilder = new ConfigurationBuilder()
        .SetBasePath(contentRoot)
        .AddJsonFile("appsettings.Development.json");

    var webHostBuilder = new WebHostBuilder()
        .UseContentRoot(contentRoot)
        .ConfigureServices(InitializeServices)
        .UseConfiguration(configurationBuilder.Build())
        .UseEnvironment("Development")
        .UseStartup(typeof(Startup));

    // Create instance of test server
    _server = new TestServer(webHostBuilder);

    Client = _server.CreateClient();
    MarketRepository = _server.Services.CreateScope().ServiceProvider.GetService<IMarketRepository>();
    Client.BaseAddress = new Uri("http://localhost:44347");
    Client.DefaultRequestHeaders.Accept.Clear();
    Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}

And cleans it up after one test class has finished running all tests.

public void Dispose()
{
    Client?.Dispose();
    _server?.Dispose();
}

When I start the tests, the first test class runs fine, but all others fail with the same error message while trying to create the test server.

Message: System.AggregateException : One or more errors occurred. (Cannot access a disposed object. Object name: ‘LoggerFactory’.) (The following constructor parameters did not have matching fixture data: TestFixture fixture) ---- System.ObjectDisposedException : Cannot access a disposed object. Object name: ‘LoggerFactory’. ---- The following constructor parameters did not have matching fixture data: TestFixture fixture Stack Trace: ----- Inner Stack Trace #1 (System.ObjectDisposedException) ----- LoggerFactory.CreateLogger(String categoryName) MicrosoftLoggingProvider.GetLogger(String name) LogProvider.GetLogger(String name) LogProvider.GetLogger(Type type, String fallbackTypeName) XMLSchedulingDataProcessor.ctor(ITypeLoadHelper typeLoadHelper) ContainerConfigurationProcessor.ctor(ITypeLoadHelper typeLoadHelper, IOptions`1 options)

When I don’t start the scheduler, the tests run fine again.

I’m using this version:

<PackageReference Include="Quartz" Version="3.2.4" />
<PackageReference Include="Quartz.Extensions.DependencyInjection" Version="3.2.4" />
<PackageReference Include="Quartz.Extensions.Hosting" Version="3.2.4" />

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ruwentoscommented, Mar 24, 2021
1reaction
ruwentoscommented, Mar 23, 2021

I will think about it. Thank you very much.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Differences between Microsoft.AspNetCore.Mvc.Testing ...
Testing package provides infrastructure to enable functional testing of applications (including the TestServer ) using the WebApplicationFactory ...
Read more >
Integration tests in ASP.NET Core
ASP.NET Core supports integration tests using a unit test framework with a test web host and an in-memory test server.
Read more >
ASP.NET Core 2.1 integration tests - Build/Test Issues
ASP.NET Core 2.1 integration tests: Hi, I'm trying to run the sample ... The current situation with pre-release dependency conflicts seems to create...
Read more >
Integration Testing in ASP.NET Core
We are going to learn about Integration testing in ASP.NET Core, and how to use WebApplicationFactory class that helps in the process.
Read more >
Integration testing AWS Lambda C# Functions with Lambda ...
Using Lambda Test Server to integration test your C# AWS Lambda functions for .NET Core locally when using a custom runtime.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found