bUnit-dev / bUnit

bUnit is a testing library for Blazor components that make tests look, feel, and runs like regular unit tests. bUnit makes it easy to render and control a component under test’s life-cycle, pass parameter and inject services into it, trigger event handlers, and verify the rendered markup from the component using a built-in semantic HTML comparer.
https://bunit.dev
MIT License
1.13k stars 105 forks source link

Trying to figure out how to test Component based on InputBase #143

Closed bfmsoft closed 4 years ago

bfmsoft commented 4 years ago

Trying to figure out how to get a Component based on InputBase to work under Bunit. I have included a test solution that I have close but can't get the test to work. The stripped down project works.

This is all based on the latest Blazor Client and pretty simple striped down project.

EnumTest.zip

bfmsoft commented 4 years ago

Cant get sample to compile 100% think on right path with cascading parameters.

egil commented 4 years ago

Hi @bfmsoft

Your first step is to get your component project to compile. When you figure out how to use your component in an <EditForm>, you are probably much closer to figuring out how to render it in a test. The two cases are most likely very similar.

When you figure out how to pass parameters to your component, the test might look a bit like this:

@inherits TestComponentBase

<Fixture Test="EnumInputSelectTest1">
    <ComponentUnderTest>
        <EditForm Model=@testModel>
            <EnumInputSelect ... />
        </EditForm>
    </ComponentUnderTest>
    @code
    {
        private EnumTestModel testModel = new EnumTestModel();

        void EnumInputSelectTest1(Fixture fixture)
        {

        }
    }
</Fixture>

See more about razor based tests here: https://bunit.egilhansen.com/docs/getting-started/writing-razor-tests.html

If you need help getting your component to work, I can recommend asking for help in https://gitter.im/aspnet/Blazor - people are very helpful and friendly.

bfmsoft commented 4 years ago

I have tried to figure this out all weekend.

I get errors here but it works here. I must be missing something simple?

Here are the errors? Error CS1662 Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type EnumTest.Tests Error The attribute names could not be inferred from bind attribute 'bind-Value'. Bind attributes should be of the form 'bind' or 'bind-value' along with their corresponding optional parameters like 'bind-value:event', 'bind:format' etc. Error CS1660 Cannot convert lambda expression to type 'object' because it is not a delegate type EnumTest.Tests Error RZ9991 The attribute names could not be inferred from bind attribute 'bind-Value'. Bind attributes should be of the form 'bind' or 'bind-value' along with their corresponding optional parameters like 'bind-value:event', 'bind:format' etc.

egil commented 4 years ago

It could be a namespace conflict? Not sure what's going on. Have you tried adding an explicit cast?

bfmsoft commented 4 years ago

I have tried renaming var (interesting thing) when renamed it didn't rename the version in bind-value. But when I did the same thing in working normal page it found an renamed in bind-value. I did try cast. I just noticed that is returns it as a "field"?

image

bfmsoft commented 4 years ago

OK, I just figured it out. Oh programming gods thank you!

I was missing: @using EnumTest.Pages in my _Imports.razor page

Seems it didn't know enough about the component from the other project.

I found this by removing all the fields then it started complain about the Component. Then when added the fields back it worked. Man that was like 30 hours looking. LOL

egil commented 4 years ago

Gah, well done. Cheer up, it happens to all of us. I recently spent 3 hours thinking I'd broken something because debugging didn't work, only to discover I was compiling in release mode...

Did it answer your testing question as well? If so, please close the issue :-)

egil commented 4 years ago

Closing this issue for now. Let me know if you still need help with this, and we can reopen it again.