-
Trying out Razor Syntax Tests with a Blazor component that is Authorized via Role. 'Fixture' is passed to the 'Setup' method and Services can be added to fixture.Services, but i dont see anyway to AddTestAuthorization. In the Setup method, tried: But still get the following exception...Bunit.TestDoubles.MissingFakeAuthorizationException Keep getting exception: Bunit.TestDoubles.MissingFakeAuthorizationException : This test requires AuthenticationStateProvider to be supplied, because the component under test uses authentication/authorization during the test. You can fix this by calling TestContext.Services.AddAuthorization with appropriate values. More information can be found in the documentation. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @roysurles Thanks for reporting this. You are not doing anything wrong, its a small mistake in the extension method that registers the fake auth in BETA-11. Here is a workaround for now: <Fixture Test="CanAddFakeAuthToContextx" Description="Fake Auth can be added to Fixture">
<ComponentUnderTest>
<SimpleAuthView />
</ComponentUnderTest>
@code
{
void CanAddFakeAuthToContextx(Fixture fixture)
{
fixture.RenderTree.TryAdd<Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationState>();
var authCtx = new TestAuthorizationContext();
authCtx.SetNotAuthorized();
authCtx.RegisterAuthorizationServices(fixture.Services);
authCtx.SetAuthorized("TestUser", AuthorizationState.Authorized);
var cut = fixture.GetComponentUnderTest<SimpleAuthView>();
cut.MarkupMatches("Authorized!");
}
}
</Fixture> And this will work in the next release (you can try the nightly if you want this now): <Fixture Test="CanAddFakeAuthToContext" Description="Fake Auth can be added to Fixture">
<ComponentUnderTest>
<SimpleAuthView />
</ComponentUnderTest>
@code
{
void CanAddFakeAuthToContext(Fixture fixture)
{
var authContext = fixture.AddTestAuthorization();
authContext.SetAuthorized("TestUser", AuthorizationState.Authorized);
var cut = fixture.GetComponentUnderTest<SimpleAuthView>();
cut.MarkupMatches("Authorized!");
}
}
</Fixture> |
Beta Was this translation helpful? Give feedback.
Hi @roysurles
Thanks for reporting this. You are not doing anything wrong, its a small mistake in the extension method that registers the fake auth in BETA-11.
Here is a workaround for now: