Going on from the blogpost from few days earlier on Unit Testing - Effective Tips With bUnit Testing. I have extended a sample of the code snippet from earlier as seen here.
[Fact]
public void ShouldSetHasNavIconBeenClickedToTrue()
{
// Arrange
_renderedComponent = RenderComponent<Class>(parameters => parameters
.Add(p => p.HasNavIconBeenClicked, true));
// Act
var hasNavIconBeenClicked = _renderedComponent.Instance.HasNavIconBeenClicked;
// Assert
Assert.True(hasNavIconBeenClicked);
}
[Fact]
public async Task ShouldOpenNavMenuEventCallBack()
{
// Arrange
bool callbackInvoked = false;
var callback = EventCallback.Factory.Create(this, () => callbackInvoked = true);
_renderedComponent = RenderComponent<Class>(parameters => parameters
.Add(p => p.HasNavIconBeenClicked, true)
.Add(p => p.OpenNavMenuEventCallBack, callback));
// Act
await _renderedComponent.Instance.OpenNavMenuEventCallBack.InvokeAsync();
// Assert
Assert.True(callbackInvoked);
}
[Fact]
public void ShouldDismissEditWorkspaceIconButton()
{
// Arrange
var menuButton = _renderedComponent.Find("#MenuButton");
// Check Actions Menu button clickable in context
// Act
menuButton.Click();
// Assert
var editTitleComponent = _renderedComponent.Find("#EditWorkspaceTitleButton");
Assert.NotNull(editTitleComponent);
editTitleComponent.Click();
var dismissEditWorkspaceIconButton = _renderedComponent.Find("#DismissEditWorkspaceIcon");
Assert.NotNull(dismissEditWorkspaceIconButton);
}
.............
@code {
[Parameter]
public string? workareaTitle { get; set; }
[Parameter]
public string? UsersDefaultWorkAreaId { get; set; }
[Parameter]
public bool HasNavIconBeenClicked { get; set; }
[Parameter]
public EventCallback OpenNavMenuEventCallBack { get; set; }
[Parameter]
public EventCallback<string> OnDefaultWorkAreaChangedEventCallBack { get; set; }
[Parameter]
public EventCallback<string> OnWorkAreaTitleUpdateEventCallback { get; set; }
private string WorkAreaTitle = "New work area";
private bool shouldDisplayNavIcon = false;
private bool hasNavIconBeenClicked = false;
.............
protected override void OnParametersSet()
{
if (!string.IsNullOrEmpty(WorkAreaTitle))
{
workareaTitle = WorkAreaTitle;
}
hasNavIconBeenClicked = HasNavIconBeenClicked;
if (UsersDefaultWorkAreaId == _sessionState.WorkAreaId.ToString())
{
shouldShowMakeDefaultWorkAreaButton = false;
}
}
.............
}
bUnit Test Code
This is useful to know about when unit testing using bUnit is that you, in which elaborates with the YouTube video yesterday in allowing you to set parameter variables to initialise when bUnit Testing using lambda expressions, to pass the value into the initialise unit test method.
As you can see in the code snippet, this denotes the parameter by declaring it using the [Parameter] heading at the top of each variable : -
[Parameter]
public bool HasNavIconBeenClicked { get; set; }
[Parameter]
public EventCallback OpenNavMenuEventCallBack { get; set; }
Which in tandem links to the method call protected override void OnParametersSet() which passes the values into HasNavIconBeenClicked and OpenNavMenuEventCallBack when this statement is called _renderedComponent = RenderComponent<Class>(parameters => parameters.
Another useful tip is to use instance callbacks to directly invoke public methods with unit test rendered components in bUnit, this can be done simply using the Instance notation as seen in the code such as: -
await _renderedComponent.Instance.OpenNavMenuEventCallBack.InvokeAsync();
Allowing you to directly call and invoke the public method once the _renderedComponent is initialised.
Hope that helps!