bUnit | testing library for Blazor components | Frontend Framework library
kandi X-RAY | bUnit Summary
kandi X-RAY | bUnit Summary
bUnit is a testing library for Blazor Components. Its goal is to make it easy to write comprehensive, stable unit tests. With bUnit, you can:. bUnit builds on top of existing unit testing frameworks such as xUnit, NUnit, and MSTest, which run the Blazor component tests in just the same way as any normal unit test. bUnit runs a test in milliseconds, compared to browser-based UI tests which usually take seconds to run.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of bUnit
bUnit Key Features
bUnit Examples and Code Snippets
Community Discussions
Trending Discussions on bUnit
QUESTION
I'm building a blazor webassembly site and I want to use bunit for testing purpose.
I run into trouble with components that use localization, I am getting the following error:
System.InvalidOperationException Cannot provide a value for property 'L' on type 'Path.To.Component'. There is no registered service of type 'Microsoft.Extensions.Localization.IStringLocalizer`1[Path.To.Component]'. at Microsoft.AspNetCore.Components.ComponentFactory.<>c__DisplayClass7_0.g__Initialize|1(IServiceProvider serviceProvider, IComponent component)
I already tried to install localization on the test project, but this did not work.
...ANSWER
Answered 2022-Feb-10 at 13:35I found the answer on the github discussion:
QUESTION
I recently asked a question regarding the difference between await Task.Run(StateHasChanged);
and await InvokeAsync(StateHasChanged);
in Blazor wasm here.
The conclusion was that await Task.Run(StateHasChanged);
was incorrect and should be avoided; using it would produce the same results as await InvokeAsync(StateHasChanged);
however would fall over when threads are available (The accepted answer explains in detail).
I've updated my codebase to use await InvokeAsync(StateHasChanged);
, however I've discovered there is actually a difference in outcome between the two.
Here's a minimal reproduction of the issue in my application:
Parent
...ANSWER
Answered 2021-Dec-02 at 06:53You aren't passing the new value into the Child
component. This is one way you could make it work.
QUESTION
I've recently inherited a Blazor Webassembly application, however have minimal experience with dotnet or Blazor.
Some of the components use await Task.Run(StateHasChanged)
rather than await InvokeAsync(StateHasChanged)
and I'm wondering if this is intentional.
I ask as await Task.Run(StateHasChanged);
gives me the following exception when attempting to render the component using bUnit:
System.InvalidOperationException The current thread is not associated with the Dispatcher. Use InvokeAsync() to switch execution to the Dispatcher when triggering rendering or component state.
Changing this to await InvokeAsync(StateHasChanged);
allows the component to be rendered in bUnit. However, as far as I can tell, the component functions identically when using the application for either await Task.Run(StateHasChanged)
or await InvokeAsync(StateHasChanged)
.
What is the difference between these two approaches to invoking StateHasChanged
?
ANSWER
Answered 2021-Nov-29 at 11:17as far as I can tell, the component functions identically ...
That is correct. Task.Run(job)
will run the job on the ThreadPool. In WebAssembly however there are no extra threads and the main (only) thread will have to run this job sooner or later.
In Blazor Server you do have Threads. Task.Run()
will work there, but StateHasChanged()
has to run on the main Thread. That means that
QUESTION
Due to the way that I'm trying to integrate AutoFixture with BUnit to run some Blazor tests, I need to create an IServiceProvider
that will have an internal Fixture
, and I can inject objects into that fixture to have those same objects returned when AutoFixture is request to create an object of that type.
My IServiceProvider
looks like this
ANSWER
Answered 2021-Oct-01 at 07:06The reason why Inject
doesn't infer the type of the instance but rather relies on the generic parameter is because it needs to enable scenarios where it is necessary to register values for base types or interfaces fixture.Inject(instance)
.
The way Inject
works behind the scenes is that it delegates the registration to Register(Func creator)
which then delegates it to a fixture.Customize(c => c.FromFactory(creator).OmitAutoProperties())
. So you can see how it wouldn't work when you need type inference.
You could as you said, make registrations one by one, or you could implement your own injection. Note the implementation below is a lighter implementation of the builder that would be normally generated by AutoFixture, and should cover most scenarios. If you find that some features are not working, like seeded requests, you might need a more complete implementation of the injection.
QUESTION
I'm writing a BUnit test for a Razor component that has injected dependencies like this:
...ANSWER
Answered 2021-Sep-29 at 15:41A fallback service provider added to bUnit's root service provider is invoked if the root service provider cannot resolve a GetService
request. With that info in mind, we can use Moq (or another mocking framework) and a little Reflection trickery to create a fallback service provider, which is really just something that implements the IServiceProvider
interface, that will use Moq to create a mocked version of a requested service, when its GetService
method is called.
This service provider will use Mock to create a mock of a requested service type once, and any subsequent requests will get the same type returned (they are saved in the mockedTypes
dictionary).
The reason for this is that you can retrieve the same mocked instance of a type in both your test and in the component under test, which allows you to configure the mock in your test.
The GetMockedService
extension method below makes it easy to pull out a Mock
from the service provider.
QUESTION
I have a Search
component that implements a debounce timer so it doesn't call ValueChanged
(and therefore doesn't update the property tied to it immediately).
The bUnit test doesn't appear to two way bind my value I am updating.
Test code ...ANSWER
Answered 2021-Sep-11 at 09:20I tried locally on my machine, and the test passed.
Here is a simplified version of your component, that only calls TimerElapsed_TickAsync one time per value change and not every time the timer runs out (AutoReset defaults to true), and two different ways to write the test that both pass on my machine:
QUESTION
I want to test whether a modal opens up or not with bunit. The problem is, that the modal doesn't get rendered. How to open a blazored modal with bunit?
Modal Creation in my component under test:
...ANSWER
Answered 2021-Jul-26 at 18:39The problem is that you are not rendering the component that actually does the rendering. Just passing in an IModalService
doesn't do it.
My approach would be to create a mock of IModalService
and assert that the expected method on it is called.
QUESTION
I try to use BUnit for component testing in a Blazor server-side project. I am quite new to this and tried to start a real simple setup:
...ANSWER
Answered 2021-Jun-27 at 06:09I just had to call:
QUESTION
Imagine I have the following class and I want to test both, the firstRender = false
and the firstRender=true
"path". For this sake I am using bunit and xunit.
ANSWER
Answered 2021-Jun-08 at 13:54bUnit allows you to perform multiple renders of a component under test. The first render will always have firstRender
set to true
, and any additional renders will have it set to false
, just like the regular Blazor runtime.
For example:
QUESTION
I have an inherited TestContext in BUnit and I want to add the Testauthorization.
This doesn't work:
...ANSWER
Answered 2021-Jun-07 at 11:15Since AddTestAuthorization
is an extension method, you need to use this
to get to it. Its an unfortunate limitation in the C# language.
E.g.:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install bUnit
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page