-
I started playing around with this fantastic project. I am trying to do tests for @typeparam TItemValue
@typeparam TItem
<h1>test</h1>
@code {
private IEnumerable<TItem> _datasource;
[Parameter]
public IEnumerable<TItem> DataSource
{
get => _datasource;
set
{
_datasource = value;
if (AllowNotify)
OnDataSourceChanged?.Invoke();
}
}
[Parameter] public bool AllowNotify { get; set; } = true;
[Parameter] public TItemValue Value { get; set; }
[Parameter] public Action OnDataSourceChanged { get; set; }
} And this test @inherits TestContext
@code {
record Person(int Id, string Name);
List<Person> _persons = new List<Person>
{
new Person(1, "John"),
new Person(2, "Lucy"),
};
[Fact]
public async Task Action_fired_when_datasource_changed()
{
bool handlerCalled = false;
Action OnDataSourceChangedHandler = () => handlerCalled = true;
var cut = Render<AntComponent<int, Person>>(
@<AntComponent DataSource="@_persons"
Value="0"
OnDataSourceChanged="OnDataSourceChangedHandler">
</AntComponent>);
_persons = new List<Person>();
Assert.True(handlerCalled);
}
} What do I need to put in the test, that the cut.WaitForState(() => object.ReferenceEquals(cut.Instance.DataSource, _persons)); but it did not work. cut.Instance.DataSource = new List<Person>(); but I think this is not the right way (besides, VS warns me I should not access the property like this). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Hi @anddrzejb, Interesting problem. I would suspect that
would call the setter of My best guess is that If you want this functionality to work consistently, then you problably need to implement the setter logic inside either If you want to verify this, you can always set breakpoints in your code and just debug your tests. |
Beta Was this translation helpful? Give feedback.
Hi @anddrzejb,
Interesting problem. I would suspect that
would call the setter of
DataSource
duringSetParametersAsync
like normal. If it didn't, then there is something fundamentally wrong with bUnit.My best guess is that
DataSource
is set beforeOnDataSourceChanged
, thus meaning property in the componentOnDataSourceChanged
is null whenDataSource
…