Giter Site home page Giter Site logo

Comments (6)

linkdotnet avatar linkdotnet commented on August 26, 2024

Hey @allderidge,

this is unfortunately a limitation we can't overcome. The Blazor renderer doesn't await DisposeAsync, therefore DisposeComponents isn't blocking until DisposeAsync is done.

Therefore you have to find other ways of telling whether or not DisposeAsync is done (in the worst: Task.Delay(10);).

from bunit.

egil avatar egil commented on August 26, 2024

Yeah, unfortunately, we do not currently have a hook into the Blazor renderere that allows us to await async disposal, as @linkdotnet says.

That said, you can probably test the logic you need to test in isolation by calling DisposeAsync yourself:

@inherits TestContextWrapper

@code
{
    [Test]
    public async Task DisposeAsync_Is_Called_When_Disposing_Components()
    {
        TestContext = new Bunit.TestContext();
        TestContext.JSInterop.Mode = JSRuntimeMode.Loose;
        var sut = Render(@<Bug />);
        var instance = sut.FindComponent<Bug>().Instance;

        await instance.DisposeAsync();

        // DisposeComponents();
        Assert.IsTrue(instance.WasDisposed);
    }
}

from bunit.

allderidge avatar allderidge commented on August 26, 2024

Thanks, it does looks like there's very little that can be done other than going into a loop which waits and tests for the outcome until some timeout.

I was thinking that these issues could be solved by implementing your own SynchronizationContext used to wait for all spawned tasks to be processed.

from bunit.

egil avatar egil commented on August 26, 2024

We are actively investigating options like that for V2 of bUnit, but it will be hard to do it for V1 (breaking changes etc.

from bunit.

linkdotnet avatar linkdotnet commented on August 26, 2024

Done by #1415

from bunit.

linkdotnet avatar linkdotnet commented on August 26, 2024

@allderidge

We implemented the feature on our upcoming v2 of bUnit. Currently, this version is in preview, but if you feel a bit more adventurous, you can try out the latest preview: https://www.nuget.org/packages/bunit/2.0.24-preview

As it is a major version, there are breaking changes. We have a rudimentary Migration Guide: https://github.com/bUnit-dev/bUnit/blob/v2/MIGRATION.md

Every feedback helps us.

Your test from above can now be properly done via: await DisposeComponentsAsync();

from bunit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.