Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • 04:58
    dnfadmin commented #571
  • 04:56
    dnfadmin commented #571
  • 04:55
    grishat opened #571
  • Nov 25 09:11
    egil closed #570
  • Nov 24 21:44
    egil commented #567
  • Nov 24 21:34
    bonsall commented #567
  • Nov 24 20:38
    egil commented #567
  • Nov 24 20:17
    bonsall commented #567
  • Nov 24 17:29
    dependabot[bot] synchronize #570
  • Nov 24 17:29
    dependabot[bot] edited #570
  • Nov 24 17:29
    dependabot[bot] edited #570
  • Nov 24 15:13
    dnfadmin commented #570
  • Nov 24 15:13
    dependabot[bot] labeled #570
  • Nov 24 15:13
    dependabot[bot] opened #570
  • Nov 23 09:48
    dependabot[bot] closed #568
  • Nov 23 09:48
    dependabot[bot] edited #568
  • Nov 23 09:48
    dependabot[bot] commented #568
  • Nov 23 09:47
    dependabot[bot] edited #568
  • Nov 22 15:14
    dnfadmin commented #568
  • Nov 22 15:13
    dependabot[bot] labeled #568
Purendra-naik-ascendlearning-com
@Purendra-naik-ascendlearning-com
Hi @egil , I'm trying to stub or mock value to cascadingparamter with private getter accessibility, the value is passed as null while RenderComponent in unit test. Here is my sample code with approached tried
Purendra-naik-ascendlearning-com
@Purendra-naik-ascendlearning-com
image.png
Could you please help me on this? @egil
1 reply
Egil Hansen
@egilhansen:matrix.org
[m]
ctx.InvokeAsync does what the exception message asks you to do, makes sure the code runs in the remderers thread.
This page explains a bit more about the two threads that are involved during testing, the test thread and the Renderer's thread.
In some cases, it's the same, but not always, and that's when you need InvokeAsync
Purendra-naik-ascendlearning-com
@Purendra-naik-ascendlearning-com
@egilhansen:matrix.org this line worked for me. Is it good to use TryAdd for cascading param? var component = ctx.RenderComponent<MyComponent>(parameters => parameters
.TryAdd("State", value)
);
Egil Hansen
@egilhansen:matrix.org
[m]
TryAdd will only add if it's not already added.
Usually just use Add with the parameterSelector
Purendra-naik-ascendlearning-com
@Purendra-naik-ascendlearning-com
@egilhansen:matrix.org Could you please suggest on Mocking below cascading param which has private get accessor and it is inaccessible outside component so i can't directly it in Test suite class. Here is comp prop
[CascadingParameter]
public ITYPE State { private get; init; }
Egil Hansen
@egilhansen:matrix.org
[m]
Parameters in blazor should not be private as far as I know.
Ankit Garg
@ankit20071991_twitter
Yes private setter I have seen but not private getter. And if TryAdd serves your purpose I think it is fine to use. The only I see is with the implementation of TryAdd. I can not add using normal Add but can do the same thing with TryAdd
Egil Hansen
@egilhansen:matrix.org
[m]
Mocking of IState is just something you need to figure out using a mocking framework of choice. I personally like NSubstitute, but any mocking frameworks or techniques work with bunit. As @ankit20071991_twitter says, just use a parameter selector and pass in your IState mock object.
Purendra-naik-ascendlearning-com
@Purendra-naik-ascendlearning-com
@egilhansen:matrix.org @ankit20071991_twitter thanks for update on this, will try to mock ISTATE using mocking framework.
Ankit Garg
@ankit20071991_twitter
@egilhansen:matrix.org Could you please provide a way to set a value for mudselect dropdwon. I am unable to set values
1 reply
brettwinters
@brettwinters:matrix.org
[m]
…Or if mud renders a select then cut.Find(“select”).Change(“the_option”) etc - also just guessing 😂
brettwinters
@brettwinters:matrix.org
[m]
..oh and for the .Find(*) method - I recommend reading the the jquery selector user guide: “.my-class” for classes, “select”, “button” “input” etc for elements, “#my_id” for ids or “[name=‘my_attribute’]” for attributes. That covers the important ones I think …
Ankit Garg
@ankit20071991_twitter
For MudSelect validation should happen if i tab out. But it is not trigeering
Gary Chan
@c0g1t8
@egilhansen:matrix.org Does bunit work in https://blazorfiddle.com?
@ankit20071991_twitter Can you provide an example of what you are testing in https://blazorfiddle.com?
Ankit Garg
@ankit20071991_twitter
I am using <MudSelect T="string" @bind-Value="@Location.Country" Label="Country" For="@(() => Location.Country)" OffsetY="true" Margin="Margin.Dense" Disabled="@IsReadOnly" Variant="Variant.Outlined" Class="d-flex">
@foreach (Countries item in Enum.GetValues(typeof(Countries)))
{
<MudSelectItem Value="@item.ToString()">@item.ToString()</MudSelectItem>
}
</MudSelect> for dropdown. Validation is happening on click of button but not on tab
Egil Hansen
@egilhansen:matrix.org
[m]
So if you take the approach i suggested and just invoke the ValueChanged event callback. Then you don't tie your tests to MudBlazors internals, and don't test their components, which you don't want to do.
Purendra-naik-ascendlearning-com
@Purendra-naik-ascendlearning-com
@egilhansen:matrix.org @ankit20071991_twitter how to mock members of base component like for example public sealed partial class TestDerivedComponent : BaseTestComponent. Im testing TestDerivedComponent which has in turn calls BaseTestComponent members
2 replies
Egil Hansen
@egilhansen:matrix.org
[m]
Correct, TryAdd returns a bool indicating if adding succeeded, so it cannot be chained. Why are you using TryAdd and not Add?
If you really need TryAdd, the do this:
var component = ctx.RenderComponent<MyComponent>(parameters => 
{
  parameters.TryAdd("param1", "value2");
  parameters.TryAdd("param2", "value2");
});
Ankit Garg
@ankit20071991_twitter
anyone who can help with MudBlazor?
Purendra-naik-ascendlearning-com
@Purendra-naik-ascendlearning-com
image.png
@egilhansen:matrix.org I see cascading param(State) value always null where as parameter Container is having value in below expression to rendercomponent var component = ctx.RenderComponent<MyComponent>(
ComponentParameterFactory.CascadingValue(nameof(MyComponent.State), valueState),
ComponentParameterFactory.Parameter(nameof(MyComponent.Container), valueContainer)
);
1 reply
Ankit Garg
@ankit20071991_twitter
@egilhansen:matrix.org Any idea if we can reset table state of mudtable. When I click on search I want to reset the current page.
3 replies
gismofx
@gismofx
Wondering if this is by design, but using RenderComponent and CSS Isolation is not coming through
4 replies
Egil Hansen
@egilhansen:matrix.org
[m]
So I'm trying to understand the test you want to write @gismofx. Can you explain?
Ankit Garg
@ankit20071991_twitter
Is there any way to test mud overlay
Christian Oleson
@Christian-Oleson
Anybody testing a file upload in bUnit? I'm a bit stuck on getting past that. I've got a component, which is a file upload, and no matter what I do I'm not quite getting the uploader right. The component is IMatFileUpload
I've tried something like this:
var fileUploader = cut.FindComponent<MatFileUpload>(); cut.InvokeAsync(() => { fileUploader.Instance.NotifyChange(new[] { fakeFileUpload }); }); cut.Render();
but it still appears as if the file is never uploaded.
Christian Oleson
@Christian-Oleson
NVM, I was looking at this for a colleague and didn't check the mock. It was the wrong type.
1 reply
Felipe Costa Gualberto
@FelipeCostaGualberto

Hello, frieds, how are you?
I have a Console application and and trying to execute:

var cut = context.RenderComponent<IndexPage>();

My component has the code:

var result = await _http.GetFromJsonAsync<ClientSchema>("api/application/GetClientSchema/1");

When this statement executed, I don't get an error, but the program execution abruptly ends, like when you press Ctrl+Break.
Do you know how can I make this statement work inside my component? Thanks!

5 replies
Egil Hansen
@egilhansen:matrix.org
[m]
I'm always so curious when I see "Message deleted" :)
Chris Sainty
@chrissainty
lol, that was a little message from my 11 month year old who was playing with my phone! I'm not sure it would have added much value 😂
4 replies
Gary Chan
@c0g1t8
@chrissainty thanks for keeping things real 😆
2 replies
Purendra-naik-ascendlearning-com
@Purendra-naik-ascendlearning-com
@egilhansen:matrix.org Need your help on Unit testing MudBlazor IDialogService as Dialog injected in component and Dialog.Show<ModalDialog>("", parameters) does not actually render ModalDialog. I'm adding this line in Test constructor ctx.Services.AddSingleton<IDialogService>(new DialogService()). Any idea how to fix this?
Egil Hansen
@egilhansen:matrix.org
[m]
@Purendra-naik-ascendlearning-com normally when you have something like a external 3rd party service like IDialogService you register a mock or fake of that instead of the real thing, and just verify with the mock that the expected method on the service was called. That way you don't end up testing 3rd party code.
Look at NSubstitute, JustMock or Moq for a mock library.
Then you can do ctx.Services.AddSingleton<IDialogService>(mockObject)
Ivan Josipovic
@IvanJosipovic
@Purendra-naik-ascendlearning-com, MudBlazor has pretty comprehensive Tests, have a look how they teste the IDialogService here, https://github.com/Garderoben/MudBlazor/blob/0696c060fd3b952eb96b81b97ac7e8ea922ac5c8/src/MudBlazor.UnitTests/Components/DialogTests.cs
1 reply
Kevin Ciliento
@Keviento

Hi ! My team and I are pretty new with Bunit and Blazor and we were wondering if there is a clean way to assert that a component has not been rendered in an Integration test context where the component under test is always the Index component and all services are injected with their real implementation excepting the HttpClient that is mocked with the suggested NuGet.

Currently, we are relying on time to assert that its has not been rendered like the following :

Action getComponentX = () => cut.FindComponent<X>;
cut.WaitForAssertion(() =>getComponentX.Should().Throw<WaitForFailedException>(), TimeSpan.FromSeconds(3));

A concrete exemple of the production use case would be when there is an @if statement.
Also, is there any hook letting us know when the Index and all its child component has finished rendering and all the async initialization have been completed ?
Thanks !

Egil Hansen
@egilhansen:matrix.org
[m]

hi @Keviento

in general, after a call to RenderComponent (or Render), the full render tree has been created. Then you can use cut.WaitForState. Its predicate will be called after each render, so if you have one or more async renders happening after the initial render, that predicate will be called after each.

To find out if a component exists in the render tree or not (e.g. if it has been rendered), I would probably use cut.HasComponent<T>(). It returns false if there is no child component in the render tree of that type.