Questions tagged [bunit]

bUnit is a library that supports unit tests for Blazor components. bUnit builds on top of existing unit testing frameworks (such as NUnit and MSTest), so please make sure that questions asked with this tag focus on the use of bUnit itself.

Filter by
Sorted by
Tagged with
1 vote
2 answers
53 views

BUnit: How to test rendering a bound value in a razor based test

I'm trying to test rendering a property (Firstname) bound to a custom component. My real structure looks like <div> <EditForm EditContext=editCtx> <MyCustomInputComponent @bind-...
wk-done's user avatar
  • 887
1 vote
3 answers
83 views

If all "async Task Method()" calls return Task.FromResult() - does that execute synchronously?

If all async Task Method() calls return Task.FromResult() - does that execute synchronously? I am asking this in the context of writing bUnit tests for my Blazor server app. A big issue for bUnit is ...
David Thielen's user avatar
0 votes
1 answer
21 views

Why is a TextEditChange() in bUnit suddenly not working

This was working and now does not. This is for a pretty simple page. I use DevExpress and emailTextBox is a DxTextBox. This is successful var emailTextBox = allTextBoxes.Single(x => x.Instance....
David Thielen's user avatar
0 votes
1 answer
19 views

How can I accomplish IRenderedComponent.FindComponent<DxTextBox>("#myid")

In bUnit I want to make a call like this: var textbox = renderedComponent.FindComponent<DxTextBox>("#myid"); However, FindComponent<T>() does not have a method that accepts any ...
David Thielen's user avatar
0 votes
0 answers
37 views

In bUnit how do I verify an element does not exist

I have the following code: var li = renderedComponent.Find("li.validation-message"); Assert.Null(li); The Find() call is throwing an exception instead of returning null. And HasComponent<...
David Thielen's user avatar
0 votes
1 answer
27 views

Overriding a service in bUnit

I have a TestContext Setup() method that adds all the services my pages need. Very nice having one place that sets up all 30+ services. Sometimes I need a different mock service for a test. So I need ...
David Thielen's user avatar
-1 votes
1 answer
43 views

In bUnit how can I wait for OnAfterRenderAsync() in the page that is my RenderComponent to complete?

Blazor server, .NET 8 (pre-release), I have a bUnit test where I need to wait for the OnAfterRenderAsync() in the page I am testing to complete before running tests. So I need something like: var ...
David Thielen's user avatar
0 votes
0 answers
25 views

bUnit - knowing all OnInitializedAsync() have completed and IRenderedComponent.Render()

I think the first part of this question may be answered by the second part, but I'm asking each in case I don't fully understand all this. This is for a bUnit test. How do I await until OnInitAsync() ...
David Thielen's user avatar
1 vote
2 answers
33 views

bUnit - why does service.SetResult() cause a render change?

The following test (from a PluralSight course) works: [Fact] public void AsyncAwait_Parameter_IndexComponent_Test() { //Arrage string expectedStringValue = "Welcome to Blazor Unit Testing ...
David Thielen's user avatar
-1 votes
2 answers
96 views

How can I turn on/off a Loading… while a page initializes in Blazor?

I want to put up a Loading… overlay while a page is loading. If everything was in my razor page then I could make it visible as the first line in OnInitializedAsync() and hide it as the last line. But ...
David Thielen's user avatar
0 votes
0 answers
39 views

bunit mock'ing a select list selection

I'm new to bUnit. In the normal run of things, I populate a dropdown and as long as a selection is not made, then when the user presses a cancel button, it will cancel, as soon as a user makes any ...
bilpor's user avatar
  • 3,575
0 votes
1 answer
37 views

Blazor unit testing with browser restrictions

When running a Blazor app in the browser, many .NET methods will throw a PlatformNotSupportedException, for example, TcpClient.ConnectAsync(). This is fine, but I want to run unit tests that will ...
BCA's user avatar
  • 7,908
0 votes
2 answers
122 views

How to check if an element is focused using bUnit?

Suppose I have a Blazor component <MyButton/> that is implemented like this for simplicity: <button>MyButton</button> And I unit test my component with bUnit as follows: @inherits ...
Behemoth's user avatar
  • 7,216
0 votes
1 answer
127 views

Check if Element is focused - Blazor B-Unit

I am writing unittests for my component. This component looks like this: component.razor <div class="main-data-data-row" @onclick="@HandleDataRowClick"> ... <input ...
Patrick Cerny's user avatar
0 votes
1 answer
135 views

How to test if JSRuntime method ran in Blazor using bUnit?

I have this method and I want to write a bUnit test to check whether the InvokeVoidAysnc() got called. I have the button click for the Onclick event and I have it so that the if statement is true: // ...
Ayushi Amin's user avatar
2 votes
1 answer
139 views

Bunit & Blazorise Cannot consume scoped service 'Microsoft.AspNetCore.Components.IComponentActivator' from singleton 'Bunit.Rendering.WebTestRenderer

I am receiving this error to do a unit test with spec test and bunit for a project in Blazor and Blazorise: Cannot consume scoped service 'Microsoft.AspNetCore.Components.IComponentActivator' from ...
Pikachuuuu's user avatar
1 vote
0 answers
658 views

Unit testing a Blazor form with MudBlazor controls using bUnit

I'm following great documentation from bUnit, MudBlazor and an example here, but somehow my code is not behaving the way it should, so I should be making some silly mistake somewhere. This is how my ...
Ash K's user avatar
  • 2,451
0 votes
0 answers
32 views

covering Localization statement in blazer server app with bunit in dot net 7

Recently we upgraded from dot net 5 to 7 Now code coverage is not fully covered Mainly localization related code is not getting covered. Tried adding mockLocalizer.Setup(_ => _["Denied"])....
VKTiwari's user avatar
0 votes
1 answer
98 views

bUnit not updating bound property

I wrote a Blazor Client component which I'm trying to test using a bUnit .razor test. The component contains a text input that is bound to a MyValue property. The input listens to @onkeyup event and ...
dzenesiz's user avatar
  • 1,408
0 votes
2 answers
215 views

How do we invoke methods that call StateHasChanged in Blazor Bunit unit tests

Consider the following Blazor component: @page "/" <p id="message">@message</p> @code { string message = "Hello!"; public Task UpdateMessage(string ...
isxpjm's user avatar
  • 313
0 votes
1 answer
128 views

Why is my Verify.Bunit test failing and not producing a received file?

I am using the Verify.Xunit and Verify.Bunit packages in my test project but they have started failing and I can't figure out why. Example simple test - just renders a component: [Fact] public Task ...
Mister Magoo's user avatar
  • 7,847
1 vote
0 answers
189 views

Testing MudBlazor component always renders empty markup

I want to make some tests for a modal component made in blazor with mudblazor. Here's my component ModalComplejo.razor <MudDialog> <TitleContent>@Titulo</TitleContent> <...
Mauro Alvarez's user avatar
0 votes
0 answers
131 views

Mock ProtectedSessionStorage getting always same value

I'm building some unit tests for Blazor using bUnit. At the moment I'm trying to mock correctly ProtectedSessionStorage because of the usage of SessionStorage in my components. This is the ...
João Estrela's user avatar
0 votes
1 answer
163 views

BUnit's IElement.Change not triggering onchange of a Blazor 6 InputSelect component

Consider the following use of <InputSelect> (Blazor 6): Edited: added the - yes, the drop-down is populated with a few values. <InputSelect @bind-Value="Model.Partner" @onchange=&...
G. Stoynev's user avatar
  • 7,477
0 votes
1 answer
245 views

Issue attempting to write bUnit Test for MudBlazor (error rendering MudPopover)

I am currently attempting to write some unit tests using bUnit. The component, I'm rendering is failing due to an error related to the MudPopover. So I've identified the issue is when it tries to ...
ist_lion's user avatar
  • 3,169
0 votes
1 answer
94 views

Test that an InputCheckbox invokes a method

I have an InputCheckbox on a page defined as: <InputCheckbox id="my-cb" ValueChanged="@CallApi" ValueExpression="@(() => MyFlag)"> @code { public bool ...
phil's user avatar
  • 1,982
1 vote
1 answer
117 views

Is there currently a way to run Blazor bUnit tests from within the GitLab CI Pipeline?

We have a Microsoft .NET Blazor server-side application. We have incorporated bUnit testing that triggers when we run the Blazor app from within VisualStudio. Recently, we have been asked to use ...
Brian's user avatar
  • 1,756
0 votes
0 answers
173 views

bUnit Unit Test not working as expected with Async

I have the following Unit Test: [Fact] public void FetchStudents_Rendered_Test() { var testData = new List<Student>() { new Student() { ...
Ervis Trupja's user avatar
  • 2,740
0 votes
0 answers
56 views

bUnit: Check for attribute addition

Here is the code where dynamicValue might be null, that is causing attribute not to be included in the output HTML: <div data-cell-state="@dynamicValue"></div> Test: var diffs = ...
desmondische's user avatar
1 vote
1 answer
32 views

Is semantic HTML comparison supported for .cs file unit tests in bUnit?

I am wondering if semantic HTML comparison is supported using .cs files for unit tests written using bUnit. I represent a probably very small group of people that cannot write unit tests using razor ...
Ben Smith's user avatar
0 votes
0 answers
92 views

bUnit: Simple markup verification for <path> tag

Here is the SVG code: <svg> <path /> </svg> Here is the bUnit test that fails for some reason: public void Chart_Render_MarkupContainsPath() { var chart = RenderComponent<...
desmondische's user avatar
0 votes
1 answer
700 views

Filling input boxes in Bunit blazor Unit test

I have a blazor page with some input boxes and a button to submit the data. There is then code to validate and upload the data to a database after passing validation. @page "/register" <...
Zachary McArthur's user avatar
2 votes
1 answer
409 views

How to mock and setup MudBlazor.IDialogService - dialogresult using Moq?

I am writing unit test for blazor webAssembly using Bunit, Xunit and Moq. I want to mock MudBlazor.IDialogService test file var mockDialogService = new Mock<IDialogService>(); var ctx = new ...
hanushi's user avatar
  • 1,201
1 vote
0 answers
151 views

How to verify JavaScript function has called or not in blazer unit testing using moq, Bunit, Xunit

I have a js function. function scrollIntoView(elementId) { var elem = document.getElementById(elementId); if (elem) { elem.scrollIntoView({ behavior: "smooth" }); window.location....
hanushi's user avatar
  • 1,201
2 votes
0 answers
507 views

How to do a view unit test for <MudSelect>?

I am working a balzor webassembly project, I tried to write a unit test for by using Bunit, Xunit,and Moq <MudSelect T="string" AnchorOrigin="Origin.BottomCenter" Label="...
hanushi's user avatar
  • 1,201
0 votes
1 answer
185 views

Bunit - unit test downloading a file

I am working a blazer web Assembly project. I want to write a unit test a download button, When I click the button, I can generate an agreement pdf. private async Task generateServiceAgreement() { ...
hanushi's user avatar
  • 1,201
0 votes
1 answer
1k views

bUnit can not find element that matches given selecter

Using .net 6, bUnit 1.13.5 Mudblazor 6.0.17 I am trying to write unit tests for a razor component that is in a dialog box and bUnit is unable to find any of the components or class/ids in it. I am ...
Steven's user avatar
  • 5
2 votes
0 answers
112 views

Can we do unit testing controllers in Blazor WebAssembly using Bunit

Can we do unit testing for controllers/end point in Blazor WebAssembly using Bunit x unit? https://bunit.dev/ like this https://learn.microsoft.com/en-us/aspnet/web-api/overview/testing-and-debugging/...
hanushi's user avatar
  • 1,201
1 vote
1 answer
98 views

how to check 200 ok using Bunit in Blazor WebAssembly

I am new to Blazor WebAssembly and unit testing. I want to do a unit test Check whether a API returns with status code 200? Check whether the API return correct output How can I do both? I tried ...
hanushi's user avatar
  • 1,201
0 votes
1 answer
412 views

Mock or initialise EditForm and EditContext with bUnit and C#

I'm using Radzen for Blazor components and I have following form: <EditForm @ref=@_editForm Model="@_selectedItem"> <div class="row"> <div class="...
Snake Eyes's user avatar
  • 16.5k
0 votes
1 answer
787 views

Bunit + Radzen, input does not change

So for my Blazor server application I'm trying to write some BUnit tests, to validate the working of some Blazor components. I use Radzen, since they provide a lot of functionality out of the box. ...
Foitn's user avatar
  • 614
3 votes
1 answer
633 views

Test "disabled" attribute with bUnit

Please see this simplified Blazor component MyComponent: <button id="enableButton" @onclick="Enable">Enable</button> <button id="disableButton" @onclick=&...
mu88's user avatar
  • 4,433
1 vote
1 answer
462 views

AntBlazor unit testing with bUnit not working

I'm fairly new to Blazor, AntBlazor, and bUnit. When my test executes I get the following error: Object reference not set to an instance of an object. at AntDesign.Alert.OnAfterRenderAsync(Boolean ...
Gericke's user avatar
  • 2,159
1 vote
1 answer
1k views

How to test interaction with JS on a Blazor component using bUnit or any other?

There is a Blazor component that interacts with JS via JSRuntime and back from JS via dotNetObjectRef. The component has an OnInit event which is called with JS using dotNetObjectRef.invokeMethodAsync(...
abberdeen's user avatar
  • 323
0 votes
0 answers
587 views

bUnit - Cannot provide a value for property "objectService" on type "directory" There is no registered service of type "IobjectService"

I'm attempting to unit test Blazor components for UI based things (when this button is pressed, these input fields should be displayed, etc). This is how my test is constructed: [Test] public void ...
Conflab's user avatar
2 votes
1 answer
174 views

Blazor Webassembly Bunit test changing blazorCulture?

I am trying to test the blazorCulture change using Bunit Test but I am unable to get and set the blazorCulture. Below code works in the actual blazor Wasm application but not working within a Bunit ...
Emil's user avatar
  • 6,571
0 votes
0 answers
421 views

How to bind a property value while rendering a blazor component in bUnit test case

I've a StoreTable component in blazor server app where I've placed a condition before rendering few controls public ProductDetailsDto ProductList { get; set; } = new ProductDetailsDto(); public ...
LogicalDesk's user avatar
  • 1,257
1 vote
1 answer
1k views

Why is bUnit telling me it cannot "click" a button because the element does not have an event handler for onclick, even though it does?

This is the element as it looks in its .RAZOR page: <button id="@(theID)" type="button" class="btn btn-primary" onclick="window.open('https://www.google.com/');&...
Brian's user avatar
  • 1,756
0 votes
1 answer
446 views

Set property in Bunit without parameter

I have an abstract base class I am trying to test that has parameters and a property that is used for @ref. I can't find a way to pass in my property to my Bunit renderer. I use this property for ...
thalacker's user avatar
  • 2,478
1 vote
1 answer
2k views

How can I access an input elements id or name from an AngleSharp.Dom.INode while bUnit testing?

I am attempting to test our Blazor project with bUnit and am having issues with a bUnit Assert. This is the component I am trying to test: <div> foreach(KeyValuePair<String, String> entry ...
Brian's user avatar
  • 1,756