I am attempting to mock a public async method in C#, using Moq, as part of the testing setup for the rendering of a Blazor Component, but the code within the method that is being mocked is being called. This seems like a very straight forward setup so I am unclear as to why the mocking of this method is not functioning as intended.
I have removed/edited the code to maintain confidentiality of this project but the error is occurring because _authenticationStateProvider.GetAuthenticationStateAsync()
is being called in Service.MockedMethod()
. The error is System.NullReferenceException: ‘Object reference not set to an instance of an object.’ because authState
is set to null
MyComponentTests.cs File
internal class MyComponentTests : BunitTestContext
{
private Mock<AuthenticationStateProvider> _mockServiceProvider;
private Mock<IAuthenticationStateService> _mockAuthenticationStateService;
[SetUp]
public void Setup()
{
_mockServiceProvider = new Mock<AuthenticationStateProvider>();
_mockAuthenticationStateService = new Mock<IAuthenticationStateService>();
Services.AddSingleton<IAuthenticationStateService>(new AuthenticationService(_mockServiceProvider.Object));
}
[Test]
public void MyComponentRendersCorrectly()
{
_mockAuthenticationStateService.Setup(s => s.MockedMethod()).ReturnsAsync("user");
var cut = RenderComponent<MyComponent>();
cut.MarkupMatches("<h1>TEST</h1>");
}
}
MyComponent.CS File
@inject AuthenticationStateService service
@code {
protected override void OnInitialized()
{
UserName = service.MockedMethod().Result;
}
}
Service.CS File
namespace Services;
public class AuthenticationStateService(AuthenticationStateProvider authenticationStateProvider) : IService
{
private readonly AuthenticationStateProvider _authenticationStateProvider = authenticationStateProvider;
public async Task<string> MockedMethod()
{
AuthenticationState authState = await _authenticationStateProvider.GetAuthenticationStateAsync();
ClaimsPrincipal user = authState.User;
if (user?.Identity is not null && user.Identity.IsAuthenticated && user?.Identity.Name is not null)
{
return user.Identity.Name.Split('@')[0].ToUpper();
}
else
{
return "UNKNOWN";
}
}
Blazor Test Project CS File
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<Using Include="Bunit" />
<Using Include="Bunit.TestDoubles" />
<Using Include="Microsoft.Extensions.DependencyInjection" />
<Using Include="NUnit.Framework" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Moq.EntityFrameworkCore" Version="8.0.1.2" />
<PackageReference Include="bunit" Version="1.29.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>
</Project>
user26425271 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2