1

I am new to Blazor WebAssembly and unit testing.

I want to do a unit test

  1. Check whether a API returns with status code 200?
  2. Check whether the API return correct output

How can I do both?

I tried like this

    public class CreateParticipantApiTest : TestContext
    {

        [Fact]
        public void ListAllAsyncShouldReturn200Status ()
        {
                var cut = RenderComponent<ParticipantsGetAll>(); // I face error at this line

//removed rest
        }

    }
public class ParticipantsGetAll : IEndpoint<IResult, GetAllParticipantsRequest>
{
// removed
}

Error : CS0311 The type 'ParticipantEndpoints.ParticipantsGetAll' cannot be used as type parameter 'TComponent' in the generic type or method 'TestContext.RenderComponent(params ComponentParameter[])'. There is no implicit reference conversion from 'ParticipantEndpoints.ParticipantsGetAll' to 'Microsoft.AspNetCore.Components.IComponent'. test Test\Participant\CreateParticipantApiTest.cs 27 Active

hanushi
  • 1,169
  • 2
  • 12
  • 27

1 Answers1

0

You cannot use the RenderComponent method to render something that is not a component. Your ParticipantsGetAll type is not a component. Learn more at https://bunit.dev/docs/getting-started/writing-tests

Just test your class ParticipantsGetAll like you would normally in C#. There is nothing Blazor specific about that.

Egil Hansen
  • 15,028
  • 8
  • 37
  • 54
  • Yes, I was wrong. I am new to unit testing too. I am asking How can I do unit test for ListAllAsyncShouldReturn200Status. I mean How can check an API return status code 200 not not in unit testing. Instead of `ParticipantsGetAll` I should use a component. But I want to do test for endpoint status – hanushi Dec 16 '22 at 15:16
  • Hi Egil, https://stackoverflow.com/questions/74828550/can-we-do-unit-testing-controllers-in-blazor-webassembly-using-bunit This is another question from me. Can you guide me? – hanushi Dec 17 '22 at 03:23
  • Microsoft has a lot of good docs and learning sessions on testing in .net. start with the basics, like testing a simple class library, and take it from there: https://learn.microsoft.com/en-us/visualstudio/test/unit-test-basics?view=vs-2022 – Egil Hansen Dec 17 '22 at 18:14