Can't use Microsoft.Maui.Storage.Preferences in unit testing

Dewangan, Khagesh 45 Reputation points
2023-03-18T13:19:55.1833333+00:00

Hi,

I am migrating one Xararin Forms project to MAUI. The application code works fine. On the unit testing side of it some of the places App.Properties was used which is now replaced with Microsoft.Maui.Storage.Preferences but this doesn't seem to be working in Unit testing code. It also throws error if it is used in the App code and called from the unit test method.

I am using NUnit test package.

This is the error I am getting :

Microsoft.Maui.ApplicationModel.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." at Microsoft.Maui.Storage.PreferencesImplementation.SetT\n at Microsoft.Maui.Storage.Preferences.Set(String key, String value, String sharedName)\n at Microsoft.Maui.Storage.Preferences.Set(String key, String value)\n at Medtronic.OneApp.Application.Core.Test.IntegrationTests.AppxamlTest.TestNaviagtionRestoreIsNotDoneWhenAppIsNotLaunched() in ~/App.xaml.Test.cs:231

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,943 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2024-01-15T09:33:35.2466667+00:00

    I know that this is old post, but now you can use inject : IPreferences preferences and register it in DI using :

                services.AddSingleton(PhoneDialer.Default);
    			services.AddSingleton(Preferences.Default);
    
    0 comments No comments