Hello I want to test a method on the pcl-part. The IOS-Part do other stuff than the android one.
For example the ios-part make a ModalPush and the android-part a normal one
How do you test such parts?
You really need to be able to call Xamarin.Forms.Forms.Init() before many APIs can work as expected - OnPlatform is one of these APIs.
Currently you can only call Init in platform projects (eg. iOS or Android). It doesn't work in a regular test runner.
One option is to use a test-runner that actually runs on iOS or Android, such as the Xamarin.Forms-based NUnit runner (which I have forked here).
The other alternative is manually mocking out all the calls that aren't going to work in a unit test, as @stvansolano said.
Hello @FokkeVermeulen have you tried abstracting the ModalPush action (interface preferly) and then mock it out? Moq is good one I generally use
Thanks for the answers, will test it
@FokkeVermeulen if you focusing more to be UI testing you can also consider Xamarin Test Cloud so you could even run and see your code in real devices. Here is a link:
Getting Started with Xamarin TestCloud
Please come back later and let us know which choices worked better for you
var field = typeof(Device).GetRuntimeProperty("OS");
@CraigDunn Can you point to a post or something on how to use the forked Nunit to do unit tests on a device?
@MikeScott8 I don't even think my fork is necessary any more - I just checked the original repo https://github.com/nunit/nunit.xamarin and it looks like they have more recent Xamarin.Forms support.
There are instructions to set it up in the readme. Set the platform project (eg the iOS NUnit runner) as the startup project, and make sure you've referenced the assemblies needed to run the tests.
There is another workaround.
You can use Prism.Services.RuntimePlatform instead of Device.RuntimePlatform and than simply moq IDeviceService.
The more modern way is to use Xamarin Essentials Device Information.
if (DeviceInfo.Platform == DevicePlatform.Android)
// Attention: This is code that the unit tests won't recognize
You can use the following nuget package in your Unit Test projects:https://github.com/jonathanpeppers/Xamarin.Forms.Mocks
Xamarin Inc., as a wholly-owned Microsoft subsidiary acting as a separate legal entity, adheres to the Microsoft Privacy Statement: Privacy & cookies