question

BuddhimaKudagama-7135 avatar image
0 Votes"
BuddhimaKudagama-7135 asked BuddhimaKudagama-7135 commented

Xamarin Unit Test - Xamarin.Essentials.NotImplementedInReferenceAssemblyException using Xamarin.Essentials.Connectivity

I was creating a unit test for one of my view models using xunit.

and the test fails when create an object of my ViewModel and I'm having the following error

"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."

And here is the Stack trace

at Xamarin.Essentials.Connectivity.get_PlatformNetworkAccess()
at Xamarin.Essentials.Connectivity.get_NetworkAccess()
at MyPCLProject.ViewModels.Base.BaseViewModel..ctor(INavigationService pNavigationService, IErrorHandlingService pErrorHandlingService) in /Volumes/Workspace/MyDirectory/XamarinTemplate/MyPCLProject/MyPCLProject/MyPCLProject/ViewModels/Base/BaseViewModel.cs:line 67
at MyPCLProject.ViewModels.BookDetailViewModel..ctor(INavigationService pNavigationService, IErrorHandlingService pErrorHandlingService) in /Volumes/Workspace/MyDirectory/XamarinTemplate/MyPCLProject/MyPCLProject/MyPCLProject/ViewModels/BookDetailViewModel.cs:line 18
at MyPCLProject.UnTestCore.UnitTest1.BookDetailViewModel_Initialize_Title_ShouldNotBe_Empty() in /Volumes/Workspace/MyDirectory/XamarinTemplate/MyPCLProject/MyPCLProject/MyPCLProject.UnTestCore/UnitTest1.cs:line 43

The reason for the error is I have a Connectivity property in my view model

 private bool _isConnectionAvailable =
             Connectivity.NetworkAccess == NetworkAccess.Internet
             || Connectivity.NetworkAccess == NetworkAccess.ConstrainedInternet;

When I comment above property, the test runs fine.

Here is my test method

 [Fact]
         public void BookDetailViewModel_Initialize_Title_ShouldNotBe_Empty()
         {
             using (var mock = AutoMock.GetLoose())
             {
                 Book book = new Book()
                 {
                     Author = "TestAuthor",
                     Id = "1",
                     Title = "TestTitle"
                 };
    
                 // arrange
                 BookDetailViewModel vm = new BookDetailViewModel(_navigationService.Object,_errorHandlingService.Object);
                 //vm.InitializeWith(book);
    
                 //Assert.NotEmpty(vm.Title);
                 //Assert.Equal("TestTitle", vm.Title);
    
             }
    
         }

How can I solve this issue?






dotnet-xamarin
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@BuddhimaKudagama-7135 Make sure you also installed the Nuget package in Android amd iOS project. Then clean the rebuild the solution. Also, try to set Android compile version to a higher version.

0 Votes 0 ·

I was able to solve the issue by extracting the Connectivity related code to an interface and a class.

0 Votes 0 ·

0 Answers