我想用 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 repository和 issue 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/