c# - 如何在 .NET Core 中使用 xUnit 测试 Xamarin ViewModel?

标签 c# unit-testing xamarin xamarin.forms xunit

我想用 xUnit 测试 Xamarin View 模型。在 Mac 上使用命令行构建代码时,显示以下错误:

/usr/local/share/dotnet/sdk/3.1.300/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.FrameworkReferenceResolution.targets(283,5): error NETSDK1073: The FrameworkReference 'Microsoft.WindowsDesktop.App.WPF' was not recognized

如果<GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks>在.csproj上使用,项目编译通过,但运行测试时报如下错误。

System.BadImageFormatException : Duplicate type with name 'App.<>PropertyChangedEventArgs'

View 模型如下所示(类的一部分)。 Fody 和 PropertyChanged.Fody 用于自动执行 INotifyPropertyChanged。

[AddINotifyPropertyChangedInterface]
public class ListaTarefasViewModel : ViewModelBase, IHandleViewAppearing, IHandleViewDisappearing
{

    public ListaTarefasViewModel(
        ITarefaService tarefaService,
        ITarefaRepository tarefaRepository,
        ITarefaRetornoItensRepository tarefaRetornoItensRepository,
        INotificationService notificationService,
        IUsuarioRepository usuarioRepository,
        IProdutoRepository produtoRepository)
    {
        this.tarefaService = tarefaService;
        this.tarefaRepository = tarefaRepository;
        this.notificationService = notificationService;
        this.usuarioRepository = usuarioRepository;
        this.tarefaRetornoItensRepository = tarefaRetornoItensRepository;
        this.produtoRepository = produtoRepository;
    }

    // ...
}

测试类:

public class ListaTarefasViewModelTest : IDisposable
{
    private readonly Mock<ListaTarefasViewModel> listaTarefasViewModelMock;

    public ListaTarefasViewModelTest()
    {
        listaTarefasViewModelMock = new Mock<ListaTarefasViewModel>();
    }

    public void Dispose()
    {
    }

    [Fact]
    public async Task ShouldConfigureTipoTarefaWhenInitializeAsync()
    {
        object tipoTarefa = TipoTarefaEnum.Inventario;
        await listaTarefasViewModelMock.Object.InitializeAsync(tipoTarefa);
        Assert.Equal(TipoTarefaEnum.Inventario, listaTarefasViewModelMock.Object.TipoTarefa);
    }
}

最佳答案

构建和执行错误

错误

/usr/local/share/dotnet/sdk/3.1.300/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.FrameworkReferenceResolution.targets(283,5): error NETSDK1073: The FrameworkReference 'Microsoft.WindowsDesktop.App.WPF' was not recognized

是由于使用包Rg.Plugins.Popup引起的,因为它通过 Xamarin.Forms ( Xamarin.Forms.Platform.WPF ) 依赖于 WPF。这可以通过使用 <PrivateAssets>all</PrivateAssets> 来解决。在 .csproj 上文件。
例子:

<PackageReference Include="Rg.Plugins.Popup" Version="2.0.0.3">
    <PrivateAssets>all</PrivateAssets>
</PackageReference>

关于 .csproj 的引用文件配置:Package references (PackageReference) in project files

错误

System.BadImageFormatException : Duplicate type with name 'App.<>PropertyChangedEventArgs'

通过清理整个解决方案或共享项目来解决,但这需要在任何测试之前进行。这似乎是由 Fody 引起的或 PropertyChanged.Fody .
这些是与此错误相关的问题,但目前尚未解决:issue on PropertyChanged.Fody repositoryissue on MarcStan / resource-embedder repository .

单元测试

最后,代码使用Autofac测试是用 xUnit 进行的. 该类已通过模拟从另一个模拟中获取所有依赖项进行了测试。

var tarefaService = Mock.Of<ITarefaService>();
var tarefaRepository = Mock.Of<ITarefaRepository>();
// ...
var mockListaTarefasViewModel = new Mock<ListaTarefasViewModel>(
    MockBehavior.Loose,
    tarefaService,
    tarefaRepository,
    // ..
);
mockListaTarefasViewModel
    .Setup(/* .. */)
    .Verifiable();
mockListaTarefasViewModel.Verify();

关于c# - 如何在 .NET Core 中使用 xUnit 测试 Xamarin ViewModel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62265710/

相关文章:

c# - 为什么 awating 后的代码在不同的线程上运行(即使设置了 SynchronizationContext)?

sql-server - 在单元测试之前运行脚本

xaml - Xamarin 执行 OnPlatform StaticResource 绑定(bind)的新方式

android - Xamarin 无法使用 : "Unexpected install output: cmd: Can' t find service: package"将应用程序部署到模拟器

javascript - 从 map 中的点坐标查找具有名称的谷歌地图地点

c# - 如何在不调用属性更改的情况下验证使用 IDataErrorInfo 和 WPF 更改的不同属性?

c# - 快速简便的 setter 和 setter/getter ?

objective-c - Xcode 不再识别测试

unit-testing - 我应该如何处理我们不打算修复的错误的单元测试?

c# - 如何将 Xamarin.Forms 条目绑定(bind)到非字符串类型,例如 Decimal