Issue
I'm making an app with xamarin.forms and I have an issue with unit testing part. The problem is that when the test case runs isolated it passes, but when I run all the tests it fails.
using EmpresaPersonal.Modelos;
using EmpresaPersonal.ModelosVisuales;
using EmpresaPersonal.Services;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xunit;
namespace EmpresaPersonal.Test
{
public class PruebasVMEditarContacto
{
[Fact]
public async Task GuardarContactoSeteaCorrectamenteAsync()
{
// Preparamos el editor y los eventos
var store = new MockDataStore();
var mockNavegacion = ServiciosFalsos.MockIrAtras();
var editor = new MVEditarContacto(store, mockNavegacion.Object)
{
// Initializer setters...
};
bool llamado = false;
MessagingCenter.Subscribe<MVEditarContacto, string>(this, MVEditarContacto.EventoContactoCreado, async (s, idContacto) => // This line throws a NullReferenceException
{
var contacto = await store.GetItemAsync(idContacto);
// Algunos chequeos van por acá
Assert.NotNull(contacto);
Assert.Equal(editor.Nombre.Valor, contacto.Nombre);
Assert.Equal(editor.Empresa.Valor, contacto.Empresa);
Assert.Equal(editor.TelefonoPrincipal.Valor, contacto.TelefonoPrincipal);
Assert.Equal(editor.TelefonoSecundario.Valor, contacto.TelefonoSecundario);
llamado = true;
});
editor.GuardarContacto.Execute(null);
await editor.EsperarCompletarAsync();
Assert.True(llamado, "El evento no fue llamado.");
mockNavegacion.Verify(m => m.AtrasAsync());
}
}
}
I don't use anything than MessagingCenter.Send` from the model instantiated in this chunk. I have to say that I have another test for a model that subscribes to this model, to the same event in it's constructor (the model is the subscriber).
Any ideas?
Solution
I saw that when unit testing very xamarin.forms related code such MessagingCenter, the best way is to run the tests from Xunit.Runner.Devices test runner (A.C.A. with a real and initialized xamarin.forms environment).
The best example I've found is the EShop on containers mobile app.
I've just left my project and started a new one heavily based on Prism.Forms to ease unit testing these kind of things because it is constructed on top of IOC containers.
Answered By - Fran
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.