TabbedPage in Xamarin.Forms is breaking unit tests

andrew.bater 1 Reputation point
2020-11-20T11:13:15.773+00:00

I have an MVVM project in Xamarin.Forms and I'm trying to write some unit tests to cover it. In the ViewModel I have this code to open a new page:

//1st (working) navigation
    public ICommand WardSelectedCommand => new Command(async () => await WardSelected());
    private async Task WardSelected()
    {
        var vm = _dependencyResolver.GetDependency<WardDetailViewModel>();
        var page = new WardDetailPage(vm);
        await _pageNavigationService.PushModalAsync(page);
    }
//2nd (broken in unit tests) navigation
    public ICommand PatientSelectedCommand => new Command(async() => await PatientSelected());
    private async Task PatientSelected()
    {
        var vm = _dependencyResolver.GetDependency<PatientPegViewModel>();
        var page = new PatientPegPage(vm);
        await _pageNavigationService.PushModalAsync(page);
    }

When the page in the method is a ContentPage, everything is fine and the unit tests run. When the page is a TabbedPage instead though I get the below error in the test runner, failing on the 2nd line in the 2nd example while running the constructor. There is no difference in the xaml or xaml.cs files other than the page inheriting from the TabbedPage, they are just 2 blank template pages generated in vs with no content at all. I even tried turning the PatientPegPage into a ContentPage and the unit test ran without errors, but when changing it back to TabbedPage the error came back.

The active test run was aborted. Reason: Test host process crashed : Unhandled exception. System.InvalidOperationException: You MUST call Xamarin.Forms.Init(); prior to using it.

I have tried adding the line of code suggested in the error message Xamarin.Forms.Init() but a) that seems like bad practice in a unit test and b) It doesn't compile, with the message The type or namespace 'Init' does not exist in the namespace 'Xamarin.Forms' (are you missing an assembly reference?)

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2020-11-20T13:07:49.937+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Your Models and ViewModels should hopefully not have any dependencies on Xamarin.Forms, so you should be able to unit test those with fakes/mocks as required easily enough.

    However, if you do need to unit test code that is dependent upon Xamarin.Forms

    Device.PlatformServices property must be set. This property implements Xamarin.Forms.Internals.IPlatformServices

    You can google:Unit testing Xamarin Forms. Overcome “You MUST call Xamarin.Forms.Init()” problem. This artcle explain how to fix this issue.

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our [documentation][1] to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.