Xamarin.Forms.Mocks | Library for running Xamarin.Forms inside of unit tests | Form library
kandi X-RAY | Xamarin.Forms.Mocks Summary
Support
Quality
Security
License
Reuse
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample Here
Xamarin.Forms.Mocks Key Features
Xamarin.Forms.Mocks Examples and Code Snippets
Trending Discussions on Xamarin.Forms.Mocks
Trending Discussions on Xamarin.Forms.Mocks
QUESTION
I'm trying to write unit tests for a ListView
with a custom DataTemplate
. Although the cells are rendered as expected on iOS and Android, their bound properties are null
when running NUnit tests.
The setup
To initiate Xamarin.Forms in an NUnit test project, I'm using Xamarin.Forms.Mocks. So the TestFixture
looks as follows:
using NUnit.Framework;
using Xamarin.Forms;
using Xamarin.Forms.Mocks;
namespace UnitTest
{
[TestFixture]
public class Test
{
[SetUp]
public void SetUp()
{
MockForms.Init();
}
What works
One test, which is working well, creates a new DataTemplate
with a custom StringCell
bound to a string "foo":
class StringCell : ViewCell
{
public StringCell()
{
var label = new Label();
label.SetBinding(Label.TextProperty, ".");
View = label;
}
}
[Test]
public void ViewCellWithString()
{
var content = new DataTemplate(typeof(StringCell)).CreateContent();
(content as Cell).BindingContext = "foo";
Assert.That(((content as ViewCell).View as Label).Text, Is.EqualTo("foo"));
}
As expected, the rendered content
is a ViewCell
with a View
of type Label
with the Text
"foo".
What doesn't work
A second test, however, fails: It creates a DataTemplate
of type ItemCell
bound to a custom object Item
with a bindable property Name
"bar".
class Item : BindableObject
{
public static readonly BindableProperty NameProperty = BindableProperty.Create(nameof(Name), typeof(string), typeof(Item), null);
public string Name {
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
}
class ItemCell : ViewCell
{
public ItemCell()
{
var label = new Label();
label.SetBinding(Label.TextProperty, nameof(Item.NameProperty));
View = label;
}
}
[Test]
public void ViewCellWithItem()
{
var content = new DataTemplate(typeof(ItemCell)).CreateContent();
(content as Cell).BindingContext = new Item { Name = "bar" };
Assert.That(((content as ViewCell).View as Label).Text, Is.EqualTo("bar"));
}
This test fails, because the Label's Text
property is null
.
My question
What am I doing wrong? Shouldn't the binding to a string via "."
behave similar to the binding to an Item
via NameProperty
? Or is there a better way to instantiate the view of a list view cell with binding properties for unit testing purposes?
ANSWER
Answered 2017-Jul-20 at 03:33I just found the tiny but crucial mistake in my code:
Instead of
label.SetBinding(Label.TextProperty, nameof(Item.NameProperty));
I need to write
label.SetBinding(Label.TextProperty, nameof(Item.Name));
since nameof(Item.NameProperty)
yields "NameProperty", but I need to bind to "Name".
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Xamarin.Forms.Mocks
Support
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesExplore Kits - Develop, implement, customize Projects, Custom Functions and Applications with kandi kits
Save this library and start creating your kit
Share this Page