samedi 30 décembre 2017

Testing Xamarin Navigation methods with context Binding on destination page fails

I have a problem with Xamarin framework. I am trying to test a Navigation.PushAsync method and I get an error:

"You must call Xamarin.Forms Init() prior using it"

Scenario

Just imagine a simple content page in Xamarin which looks like this in the code behind.

public partial class TesterPage : ContentPage
{


    public TesterPage()
    {
      InitializeComponent();
    }
}

And here is the XAML part for it:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage 

         xmlns="http://ift.tt/1i1Py1I"
         xmlns:x="http://ift.tt/Ten7b2"
         x:Class="BoatInspectorPMS.TesterPage">
<ContentPage.Content>
    <StackLayout>
        <Label Text="Welcome to Xamarin Forms!" />
    </StackLayout>
</ContentPage.Content>
</ContentPage>

This is out destination page. We have the origin page that looks like this partly.

public class MainViewModel
{
     private readonly IPageService _pageService;

     public MainViewModel(IPageService pageService)
     {
        _pageService = pageService;
     }

     public async Task NewSurvey()
     {
        await _pageService.PushAsync(new TesterPage());
     }
}

The interface IPageService looks like this:

public interface IPageService
{
    Task PushAsync(Page page);
}

So we want to run a test which looks like this. Here is our test.

    [Test]
    public async Task OurTest()
    {           
        //mock PageService
        var pageService = new Mock<PageService>();
        var mvm = new MainViewModel(pageService.Object);

        await mvm.NewSurvey();
        pageService.Verify(p => p.PushAsync(It.IsAny<TesterPage>()));

    }

I get an error in the constructor of TesterPage at InitializeComponent() at the moment when it enters auto generated code

  private void InitializeComponent() {
        global::Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this, typeof(TesterPage));
    }

Pretty stuck on this issue. Hope someone has come across a similar situation. Thank you.

Aucun commentaire:

Enregistrer un commentaire