I'm attempting to unit test a Xaml page via a mock using XUnit but i'm having problems with SvgCachedImage not being recognized. The error code I'm receiving is You are referencing the Portable version in your App - you need to reference the platform version
Use this test
[Fact]
public void DashboardPage_CreateApp()
{
ImageService.EnableMockImageService = true;
var svgXaml =
"<SvgCachedImage xmlns:forms=\"clr-namespace:FFImageLoading.Svg.Forms;assembly=FFImageLoading.Svg.Forms\" Source=\"resource://Project.Common.Images.Svgs.icon_3.svg?assembly=Project.Common\" HeightRequest=\"24\"/>";
SvgCachedImage image = new SvgCachedImage();
image.LoadFromXaml(svgXaml);
Assert.Equal(24,image.HeightRequest);
}
The test should pass
The test throws an "You are referencing the Portable version in your App - you need to reference the platform version"
I can appreciate the amount of people who ask questions without RTFM, but I've done my homework on this. I am aware of the ImageServer.EnableMockImageService property, and it is working when running a test to see if the ImageService is initialized (like in your sample).
However, I'm testing xaml pages and it is not working. I have isolated the issue to a very small test by simply creating a control from xaml inside a test (like the code I have in the initial post).
This passes
[Fact]
public void CanInitialize()
{
ImageService.Instance.Initialize();
Assert.NotNull(ImageService.Instance.Config);
}
This fails (within the same class where ImageServer.EnableMockImageService set to true
[Fact]
public void LoadXamlTest()
{
var svgXaml =
"<SvgCachedImage xmlns:forms=\"clr-namespace:FFImageLoading.Svg.Forms;assembly=FFImageLoading.Svg.Forms\" Source=\"resource://PROJECTNAME.Images.Milliman_logo.svg?assembly=PROJECTNAME\" HeightRequest=\"24\"/>";
var image = new SvgCachedImage();
image.LoadFromXaml(svgXaml);
Assert.Equal(24, image.HeightRequest);
}
The error full error is:
System.Exception : You are referencing the Portable version in your App - you need to reference the platform version
[xUnit.net 00:00:00.99] Stack Trace:
[xUnit.net 00:00:00.99] at FFImageLoading.Svg.Forms.SvgImageSource..ctor(ImageSource imageSource, Int32 vectorWidth, Int32 vectorHeight, Boolean useDipUnits, Dictionary`2 replaceStringMap)
[xUnit.net 00:00:00.99] at FFImageLoading.Svg.Forms.SvgCachedImage.CoerceImageSource(Object newValue)
[xUnit.net 00:00:00.99] D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs(433,0): at Xamarin.Forms.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes)
[xUnit.net 00:00:00.99] D:\a\1\s\Xamarin.Forms.Xaml\ApplyPropertiesVisitor.cs(502,0): at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.TrySetValue(Object element, BindableProperty property, Boolean attached, Object value, IXmlLineInfo lineInfo, XamlServiceProvider serviceProvider, Exception& exception)
[xUnit.net 00:00:00.99] D:\a\1\s\Xamarin.Forms.Xaml\ApplyPropertiesVisitor.cs(335,0): at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.SetPropertyValue(Object xamlelement, XmlName propertyName, Object value, Object rootElement, INode node, HydrationContext context, IXmlLineInfo lineInfo)
[xUnit.net 00:00:00.99] D:\a\1\s\Xamarin.Forms.Xaml\ApplyPropertiesVisitor.cs(57,0): at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.Visit(ValueNode node, INode parentNode)
[xUnit.net 00:00:00.99] D:\a\1\s\Xamarin.Forms.Xaml\XamlNode.cs(199,0): at Xamarin.Forms.Xaml.RootNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
[xUnit.net 00:00:00.99] D:\a\1\s\Xamarin.Forms.Xaml\XamlLoader.cs(89,0): at Xamarin.Forms.Xaml.XamlLoader.Load(Object view, String xaml)
[xUnit.net 00:00:00.99] D:\a\1\s\Xamarin.Forms.Xaml\ViewExtensions.cs(43,0): at Xamarin.Forms.Xaml.Extensions.LoadFromXamlTXaml
Thanks for your help.
I have the same problem. Any XAML page that uses FFImageLoading cannot be unit tested as it throws
System.Exception : You are referencing the Portable version in your App - you need to reference the platform version
Any help is appreciated.
Most helpful comment
I have the same problem. Any XAML page that uses FFImageLoading cannot be unit tested as it throws
System.Exception : You are referencing the Portable version in your App - you need to reference the platform versionAny help is appreciated.