C# MessagingCenter回调无法注册
我正在用xamarin.forms制作一个应用程序,我对单元测试部分有一个问题。问题是,当测试用例独立运行时,它通过了测试,但当我运行所有测试时,它失败了C# MessagingCenter回调无法注册,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我正在用xamarin.forms制作一个应用程序,我对单元测试部分有一个问题。问题是,当测试用例独立运行时,它通过了测试,但当我运行所有测试时,它失败了 using EmpresaPersonal.Modelos; using EmpresaPersonal.ModelosVisuales; using EmpresaPersonal.Services; using System; using System.Collections.Generic; using System.Text; usi
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());
}
}
}
使用EmpresaPersonal.Modelos;
使用EmpresaPersonal.ModelosVisuales;
使用个人服务;
使用制度;
使用System.Collections.Generic;
使用系统文本;
使用系统线程;
使用System.Threading.Tasks;
使用Xamarin.Forms;
使用Xunit;
名称空间为personal.Test
{
公共类PruebasvMeditaro Contacto
{
[事实]
公共异步任务GuardContactOSeTeaCorrectionEasync()
{
//准备编辑和事件
var store=new MockDataStore();
var mockNavegacion=ServiciosFalsos.MockIrAtras();
var editor=new MVEditarContacto(store,mockNavegacion.Object)
{
//初始化设置程序。。。
};
bool-llamado=false;
Subscribe(this,MVEditarContacto.eventocontactocredo,async(s,idContacto)=>//此行引发NullReferenceException
{
var contacto=await store.GetItemAsync(idContacto);
//阿尔古诺斯·切克奥斯·范波拉卡
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);
拉马多=真;
});
editor.guardarcontact.Execute(null);
等待编辑器。EsperarCompletarAsync();
断言。正确(llamado,“事件不可能发生。”);
mockNavegacion.Verify(m=>m.AtrasAsync());
}
}
}
我只使用MessagingCenter.Send`这个模块中实例化的模型。我必须说,我对订阅此模型的模型进行了另一个测试,订阅了它的构造函数中的同一事件(该模型就是订阅方)
有什么想法吗?我看到,当单元测试非常与xamarin.forms相关的代码(如MessagingCenter)时,最好的方法是从Xunit.Runner.Devices test Runner(A.C.A.使用真实且初始化的xamarin.forms环境)运行测试 我发现的最好的例子就是移动应用程序
我刚刚离开我的项目,开始了一个基于Prism.Forms的新项目,以简化这类东西的单元测试,因为它是在IOC容器上构建的。Hi,正在尝试用
对象
替换MVEditarContacto
。谢谢,我会试试的。我的意思是在这里回复。MessagingCenter。订阅
,如果有用请告诉我。