Essentials: Recommendation for Xamarin.Essentials with Unit Testing

Created on 24 Sep 2018  路  8Comments  路  Source: xamarin/Essentials

This is more of a question rather than a bug report.

I started using Xamarin.Essentials in a new app, I've historically used the various plugins for cross platforms I've needed. So very excited for Xamarin.Essentials and the consolidated location for all these cross platform features.

However, one nice thing with the older plugin model provided was each feature being wrapped in an interface. This meant that any code (specifically view models) interacting with the plugins could easily be unit tested by providing a mock for the plugin.

Today, as I was adding tests for my view models I realized that it won't be as simple to create unit tests for code that depends on Xamarin.Essentials. The test assembly encounters the following exception (I know this is by design).
Xamarin.Essentials.NotImplementedInReferenceAssemblyException : This functionality is not implemented in the portable version of this assembly. You should reference the NuGet package from your main application project in order to reference the platform-specific implementation.

Is the recommendation here to wrap all of my Xamarin.Essentials interactions in my own interfaces? I was hoping to be able to avoid that if possible.

Most helpful comment

We have guidance on our wiki: https://github.com/xamarin/Essentials/wiki/FAQ-%7C-Essentials#where-are-the-interfaces

All 8 comments

May be this can help

We have guidance on our wiki: https://github.com/xamarin/Essentials/wiki/FAQ-%7C-Essentials#where-are-the-interfaces

Thanks @jamesmontemagno missed that in the docs. Makes sense to do it that way, was just a little disappointed to discover that it code using xamarin.essentials can't be executed from a unit test library.

Is that something that might ever be on the road map in the future?

We have no plans to add any interfaces to the library and have our recommendations.

We may be adding a few more properties to help detect if the API is available on the platform it is running in.

@jamesmontemagno Agreed to the considerations and recommendations written in guidance. I was looking for a way to check if the code was executing on an unsupported platform, but even that call is unsupported. So a few properties to check if something is available would be good.

I.e I was trying to get a cache/temp directory in a simple C# class, that I wanted to do some testing against:

CacheDirectory = (DeviceInfo.Platform == DeviceInfo.Platforms.Unsupported) ? 
    Path.GetTempPath() : FileSystem.CacheDirectory;

But that is not possible as even the DeviceInfo is not available outside a device context.

I opened a new issue for this: https://github.com/xamarin/Essentials/issues/619

I think would be a good idea.

Just installed Essentials into my projects and came across this.
Unit testing a section of data handling code that accesses a file in the AppDirectory thorws this exception.
I guess that I'll have to revert to my old code, that worked.
Will this mean that any use of Essentials by a section of code mean that it's not unit testable? Big failing if that is the case.

@brandwooddixon please read through: https://github.com/xamarin/Essentials/wiki/FAQ-%7C-Essentials#where-are-the-interfaces

You can also use http://essential-interfaces.azurewebsites.net/ to generate interfaces for you, which is a community project.

Was this page helpful?
0 / 5 - 0 ratings