BlazorGoogleMaps
BlazorGoogleMaps copied to clipboard
How to mock the map component for tests?
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?
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>
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
Closing due inactivity. Feel free to open.