All Projects → jonathanpeppers → Xamarin.forms.mocks

jonathanpeppers / Xamarin.forms.mocks

Licence: mit
Library for running Xamarin.Forms inside of unit tests

Projects that are alternatives of or similar to Xamarin.forms.mocks

Xamarin.forms
Xamarin.Forms Official Home
Stars: ✭ 5,485 (+2964.25%)
Mutual labels:  hacktoberfest, xamarin, xamarin-forms
Hotreload
Xamarin.Forms XAML hot reload, live reload, live xaml
Stars: ✭ 407 (+127.37%)
Mutual labels:  hacktoberfest, xamarin, xamarin-forms
Open Source Xamarin Apps
📱 Collaborative List of Open Source Xamarin Apps
Stars: ✭ 318 (+77.65%)
Mutual labels:  hacktoberfest, xamarin, xamarin-forms
Reactiveui
An advanced, composable, functional reactive model-view-viewmodel framework for all .NET platforms that is inspired by functional reactive programming. ReactiveUI allows you to abstract mutable state away from your user interfaces, express the idea around a feature in one readable place and improve the testability of your application.
Stars: ✭ 6,709 (+3648.04%)
Mutual labels:  hacktoberfest, xamarin, xamarin-forms
Plugin.audiorecorder
Audio Recorder plugin for Xamarin and Windows
Stars: ✭ 140 (-21.79%)
Mutual labels:  xamarin, xamarin-forms
Chameleon
Chameleon is a flexible media player build with Xamarin.Forms
Stars: ✭ 137 (-23.46%)
Mutual labels:  xamarin, xamarin-forms
Xamarin Forms Carouselview
A Xamarin.Forms custom control recipe that mirrors the functionality of the CarouselPage - except the "pages" are simply Content Views.
Stars: ✭ 147 (-17.88%)
Mutual labels:  xamarin, xamarin-forms
Graphicscontrols
Experimental GraphicsControls - Build drawn controls (Cupertino, Fluent and Material)
Stars: ✭ 149 (-16.76%)
Mutual labels:  xamarin, xamarin-forms
Plugin.localnotification
The local notification plugin provides a way to show local notifications from Xamarin Forms apps .
Stars: ✭ 124 (-30.73%)
Mutual labels:  xamarin, xamarin-forms
Prism.plugin.popups
This provides extensibility for Prism.Forms INavigationService to handle Popup Views
Stars: ✭ 149 (-16.76%)
Mutual labels:  hacktoberfest, xamarin-forms
Xamarines
🕹️📱Cross-Platform Nintendo Emulator using Xamarin and .Net Standard!
Stars: ✭ 153 (-14.53%)
Mutual labels:  xamarin, xamarin-forms
Xamarin Docs
Xamarin Documentation - public content repo
Stars: ✭ 136 (-24.02%)
Mutual labels:  xamarin, xamarin-forms
Xamarin.forms.breadcrumb
This is a breadcrumb navigation control that is complete automatic and uses the Navigation stack and page titles to generate the breadcrumbs.
Stars: ✭ 130 (-27.37%)
Mutual labels:  xamarin, xamarin-forms
Xamarin.forms.videoplayer
A Xamarin Forms control to render the native video player on every platform.
Stars: ✭ 140 (-21.79%)
Mutual labels:  xamarin, xamarin-forms
Arcgis Toolkit Dotnet
Toolkit for ArcGIS Runtime SDK for .NET
Stars: ✭ 125 (-30.17%)
Mutual labels:  xamarin, xamarin-forms
Simpleauth
The Simplest way to Authenticate and make Rest API calls in .Net
Stars: ✭ 148 (-17.32%)
Mutual labels:  xamarin, xamarin-forms
Glidex
glidex.forms is a library using Glide for faster Xamarin.Forms images on Android. Find out more about Glide at https://github.com/bumptech/glide
Stars: ✭ 162 (-9.5%)
Mutual labels:  hacktoberfest, xamarin
Xamarin.plugin.calendar
Calendar plugin for Xamarin.Forms
Stars: ✭ 159 (-11.17%)
Mutual labels:  xamarin, xamarin-forms
Mobile.buildtools
The Mobile.BuildTools makes it easier to develop code bases in a clean, consistent, secure, and configurable way. Determine at Build which environment your app needs to run on, and what Client Secrets it should have. Plus many more amazing features!
Stars: ✭ 162 (-9.5%)
Mutual labels:  hacktoberfest, xamarin
Xamarin Forms Goodlooking Ui
Xamarin.Forms goodlooking UI samples
Stars: ✭ 2,300 (+1184.92%)
Mutual labels:  xamarin, xamarin-forms

Xamarin.Forms.Mocks

Library for running Xamarin.Forms inside of unit tests

NuGet Windows / AppVeyor OS X / Travis-CI
NuGet AppVeyor Travis CI

If you've ever written any complicated logic inside a Xamarin.Forms View, you quickly realize that this code can't be unit tested easily. Your colleagues will tell you to MVVM all the things, but you cannot get around interacting with Xamarin.Forms itself. Things like navigation, animations, custom markup extensions, etc. can become an untested mess.

If you are determined to try it, you will probably do something like this and then give up: FAIL

You can now install this package from NuGet and get past this issue:

[SetUp]
public void SetUp()
{
    Xamarin.Forms.Mocks.MockForms.Init();
}

You can even do things in unit tests like load XAML dynamically:

using Xamarin.Forms.Xaml;
//...
[Test]
public void LoadFromXaml()
{
    var label = new Label();
    label.LoadFromXaml("<Label Text=\"Woot\" />");
    Assert.AreEqual("Woot", label.Text);
}

You can unit test navigation:

[Test]
public async Task Push()
{
    var root = new ContentPage();
    var page = new ContentPage();
    await root.Navigation.PushAsync(page);
    Assert.AreEqual(root.Navigation.NavigationStack.Last(), page);
}

You can unit test animations:

[Test]
public async Task FadeTo()
{
    var view = new BoxView();
    await view.FadeTo(0);
    Assert.AreEqual(0, view.Opacity);
}

You can even unit test your markup extensions:

public class TerribleExtension : IMarkupExtension<string>
{
    public string ProvideValue(IServiceProvider serviceProvider)
    {
        return "2016";
    }

    object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
    {
        return ProvideValue(serviceProvider);
    }
}

[Test]
public void MarkupExtension()
{
    var label = new Label();
    label.LoadFromXaml("<Label xmlns:f=\"clr-namespace:Xamarin.Forms.Mocks.Tests;assembly=Xamarin.Forms.Mocks.Tests\" Text=\"{f:Terrible}\" />");
    Assert.AreEqual("2016", label.Text); //amirite?
}

You can unit test persistence of Application properties:

[Test]
public async Task SaveAndLoad()
{
    var app = new App();
    app.Properties["Chuck"] = "Norris";
    await app.SavePropertiesAsync();

    app = new App();
    Assert.AreEqual("Norris", app.Properties["Chuck"]);
}

You can assert Device.OpenUri was called properly:

[Test]
public void OpenUri()
{
    var expected = new Uri("https://www.google.com");
    var actual = default(Uri);

    MockForms.OpenUriAction = u => actual = u;    
    Device.OpenUri(expected);
    Assert.AreEqual(expected, actual);
}

You can use Device.StartTimer as you would expect:

[Test]
public async Task StartTimer()
{
    var source = new TaskCompletionSource<bool>();
    Device.StartTimer(TimeSpan.FromMilliseconds(1), () =>
    {
        source.SetResult(true);
        return false;
    });

    Assert.IsTrue(await source.Task);
}

To test things using Application.Current or its resources:

[SetUp]
public void SetUp()
{
    MockForms.Init();

    //This is your App.xaml and App.xaml.cs, which can have resources, etc.
    Application.Current = new App();
}

[TearDown]
public void TearDown()
{
    Application.Current = null;
}

How does it work?

The main issue with trying to call Xamarin.Forms.Init() yourself for unit testing is that all kinds of interfaces and classes are marked internal. I get around this by conforming to [assembly: InternalsVisibleTo] which is declared for the purposes of unit testing Xamarin.Forms itself.

I merely named the output assembly Xamarin.Forms.Core.UnitTests.dll, and the MockForms class is able to call internal stuff all it wants. Just remember marking something internal does not mean someone tricky can't call it if they are determined enough.

I patterned after unit tests in Xamarin.Forms itself to figure out how to most easily mock everything.

Notes

Device.BeginInvokeOnMainThread is currently just synchronous. This may not be desired, but is the quickest plan for now.

All animations will just complete immediately. Just await them and use async unit tests.

I tested everything with NUnit, but nothing is tied specifically to it. Things should work find if you want to use a different unit testing library.

Xamarin.Forms versions

Make sure to choose the appropriate version of this package for the version of Xamarin.Forms you are using:

Xamarin.Forms Xamarin.Forms.Mocks
Any Newer Try latest & file issues, thanks!
4.7.0.x 4.7.0.x
4.6.0.x 4.6.0.x
3.5.x-4.5.x 3.5.0.x
3.0.0.x 3.0.0.x
2.5.0.x 2.5.0.x
2.4.0.x 2.4.0.x
2.3.5.x-pre 2.3.5.x-pre6
2.3.4.x 2.3.4.x
2.3.3.x 2.3.3.1
2.3.0-2.3.2 0.1.0.4
Older Unsupported

As another option, you can include the source code for this project along with your unit tests. This allows you to target any version of Xamarin.Forms desired assuming there are no code changes.

Wish List

  • I am not happy with Device.BeginInvokeOnMainThread being synchronous.
  • There are certainly other Xamarin.Forms internals not implemented. Let me know if there is something missing you need.
  • I back-dated this lib to support Xamarin.Forms 2.3.x, although it may be able to go back further. It is hard to know how often the forms team changed some of these internal interfaces.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].