rungwiroon / BlazorGoogleMaps

Blazor interop for GoogleMap library
MIT License
329 stars 106 forks source link

How to mock the map component for tests? #197

Closed jakob-reesalu closed 1 year ago

jakob-reesalu commented 2 years ago

Hi!

I need to mock the GoogleMap class for testing and it sort of works, up to one little detail, namely having the GoogleMap.InteropObject return a value, or just not be null. Because, in my code I must do the below null check on the InteropObject, in OnAfterRenderAsync:

protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            // Using firstRender doesn't help us here. We must assert GoogleMap.InteropObject
            // has loaded, which sometimes happens AFTER firstRender. When it has loaded we
            // call StateHasChanged() triggering this method again.
            if (GoogleMap.InteropObject != null && _isLoading)
            {
               // Get a route for the map
               // Display route
            }
        }

However, InteropObject is a private set so I can't assign it a value for a test. And Moq nor FakeItEasy can mock/fake this because InteropObject doesn't have the virtual keyword, so I can't override it for testing purposes. Or, so I understand it at least.

Any help on this?

valentasm1 commented 2 years ago

For check after map render you should use MapComponent OnAfterInit event Maybe making MapComponent props virtual should do any harm. What you think?

<GoogleMap @ref="@map1" Id="map1" Options="@mapOptions" OnAfterInit="@AfterMapInit"></GoogleMap>
benjaminsampica commented 1 year ago

If you're using bUnit you can use component stubbing alongside something like Moq or NSubstitute

https://bunit.dev/docs/providing-input/substituting-components.html

valentasm1 commented 1 year ago

Closing due inactivity. Feel free to open.