Skip to content

How to test component page with @attribute [Authorize...] will not render for unauthorized user? #448

Answered by egil
kwilkins asked this question in Q&A
Discussion options

You must be logged in to vote

Hi there,

bUnit doesn't look at the authorize attribute of a component. It simply renders it if you tell it to.

Testing that Blazor respects the authorize attribute would be you testing the blazor runtime, which is probably not what you want.

If you want to test that the Admin component has the authorize attribute set correctly. I would look for the attribute on the component and assert it is there with the correct/expected values, e.g.:

var actual = Attribute.GetCustomAttribute(typeof(Admin), typeof(AuthorizeAttribute)) as AuthorizeAttribute;

Assert.NotNull(actual);
Assert.Equal("admin", actual.Roles);

(completely untested code, but that should get you started).

More here: https://docs.…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@kwilkins
Comment options

@egil
Comment options

egil Jul 3, 2021
Maintainer

@kingua
Comment options

Answer selected by kwilkins
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants