在测试中手动触发“ValueChanged”事件来更新绑定属性。代码示例:
//定义一个测试组件
public class TestComponent : ComponentBase
{
private string _testProperty;
[Parameter]
public string TestProperty
{
get => _testProperty;
set
{
_testProperty = value;
TestPropertyChanged?.Invoke(this, EventArgs.Empty);
}
}
public event EventHandler TestPropertyChanged;
}
//测试代码
[Test]
public async Task TestBindProperty()
{
//Arrange
var testProperty = "Initial Value";
var component = RenderComponent(parameters => parameters
.Add(p => p.TestProperty, testProperty)
);
var inputElement = component.Find("input");
//Act
await inputElement.ChangeAsync("Test Value");
//手动触发ValueChanged事件来更新绑定属性
component.Instance.TestPropertyChanged?.Invoke(component.Instance, EventArgs.Empty);
//Assert
component.Instance.TestProperty.Should().Be("Test Value");
}