简体   繁体   English

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

[英]How to Test Xamarin ViewModel with xUnit in .NET Core?

I want to test a Xamarin view model with xUnit.我想用 xUnit 测试 Xamarin 视图 model。 When the code is build using command line on Mac, the following error are show:在 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 /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' 未被识别

If <GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks> is used on.csproj, the project compiles, but the following error are reported when I try to run the test.如果在.csproj上使用<GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks> ,项目编译成功,但是我尝试运行测试时报以下错误。

System.BadImageFormatException: Duplicate type with name 'App.<>PropertyChangedEventArgs' System.BadImageFormatException:名称为“App.<>PropertyChangedEventArgs”的重复类型

The view model are show below (part of the class).视图 model 如下所示(类的一部分)。 Fody and PropertyChanged.Fody are used to automate the implementation of INotifyPropertyChanged. 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;
    }

    // ...
}

The test class:测试 class:

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);
    }
}

Build and Execution Errors构建和执行错误

The error错误

/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 /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' 未被识别

was caused by the use of the package Rg.Plugins.Popup , because it depends of WPF through Xamarin.Forms ( Xamarin.Forms.Platform.WPF ). was caused by the use of the package Rg.Plugins.Popup , because it depends of WPF through Xamarin.Forms ( Xamarin.Forms.Platform.WPF ). This can be resolved by using <PrivateAssets>all</PrivateAssets> on the .csproj file.这可以通过在.csproj文件上使用<PrivateAssets>all</PrivateAssets>来解决。
Example:例子:

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

Reference about the .csproj file configuration: Package references (PackageReference) in project files .csproj文件配置参考: Package 项目文件中的参考资料(PackageReference)

The error错误

System.BadImageFormatException: Duplicate type with name 'App.<>PropertyChangedEventArgs' System.BadImageFormatException:名称为“App.<>PropertyChangedEventArgs”的重复类型

was solved by cleaning the entire solution or the shared project, but this is needed to be made before any test.通过清理整个解决方案或共享项目来解决,但这需要在任何测试之前进行。 This appears to be caused by Fody or PropertyChanged.Fody .这似乎是由FodyPropertyChanged.Fody引起的。
That are issues related to the this error, but none was resolved by now: issue on PropertyChanged.Fody repository and issue on MarcStan / resource-embedder repository .这是与此错误相关的问题,但目前尚未解决: PropertyChanged.Fody 存储库上的问题和 MarcStan / resource-embedder 存储库上的问题

Unit Test单元测试

Finally, the code uses Autofac and the test was made with xUnit .最后,代码使用Autofac并使用xUnit进行测试。 The class was tested with the mock getting all the dependencies from another mocks. class 使用模拟从另一个模拟获取所有依赖项进行了测试。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM