【问题标题】:How to Test Xamarin ViewModel with xUnit in .NET Core?如何在 .NET Core 中使用 xUnit 测试 Xamarin ViewModel?
【发布时间】:2020-09-27 15:12:09
【问题描述】:

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

/usr/local/share/dotnet/sdk/3.1.300/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.FrameworkReferenceResolution.targets(283,5):错误 NETSDK1073:FrameworkReference 'Microsoft .WindowsDesktop.App.WPF' 未被识别

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

System.BadImageFormatException:名称为“App.PropertyChangedEventArgs”的重复类型

视图模型如下所示(类的一部分)。 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);
    }
}

【问题讨论】:

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


【解决方案1】:

构建和执行错误

错误

/usr/local/share/dotnet/sdk/3.1.300/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.FrameworkReferenceResolution.targets(283,5):错误 NETSDK1073:FrameworkReference 'Microsoft .WindowsDesktop.App.WPF' 未被识别

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

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

.csproj文件配置参考:Package references (PackageReference) in project files

错误

System.BadImageFormatException:名称为“App.PropertyChangedEventArgs”的重复类型

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

单元测试

最后,代码使用Autofac,测试使用xUnit。 该类已通过 mock 从另一个 mock 获取所有依赖项进行测试。

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

【讨论】:

  • 谢谢。今天发生在我身上的“Xamarin.CommunityToolkit”Version="1.1.0-pre2"
猜你喜欢
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
  • 2017-08-24
  • 2018-02-20
  • 2021-01-19
  • 2018-05-16
  • 1970-01-01
相关资源
最近更新 更多