Search code examples
unit-testingxamarinmvvmcross

MvvmCross: Unit-testing services with plugins



I am creating a cross-platform project with MvvmCross v3 and Xamarin solution and i would like to create some unit-tests. This seems a bit outdated, so i was trying to follow this and it worked as expected.

However, I am now making an attempt to unit-test some of my domain services, which are dependent on platform specific MvvvCross plugins (e.g ResourceLoader).

Running the test results in the following exception:

Cirrious.CrossCore.Exceptions.MvxException: Failed to resolve type   
Cirrious.CrossCore.Plugins.IMvxPluginManager.

I assume that IMvxPluginManager is probably registered in the Setup flow, and that I need to include platform implementation of the plugins in my project, yet I was wondering what would be the preferred way of setting up my unit-test project? Is there something that I am missing?

Is there any updated tutorial for the above task?

Are there already any plugin platform extensions that supports test environment, or should I make an attempt to write them by myself?


Solution

  • In general, you shouldn't be loading the plugins or a real MvxPluginManager during your service tests.

    Instead your unit tests should be registering mock types for the interfaces that your services need to use.

    var mock = new Mock<INeedToUse>();
    // use mock.Setup methods
    Ioc.RegisterSingleton<INeedToUse>(mock.Object);
    // or you can use constructor dependency injection on INeedToUse instead
    

    You can also register a mock IMvxPluginManager if you really need to, but in the majority of cases I don't believe you should need that. If you've got a case where you absolutely need it, please post a code sample - it's easier to talk in code than text.