【发布时间】: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);
}
}
【问题讨论】:
-
嗨罗尼!我刚刚将单元测试添加到我的 GitTrends 应用程序中,并想分享它们作为如何为 Xamarin.Forms 应用程序的 ViewModels 编写单元测试的示例:github.com/brminnick/GitTrends/tree/master/GitTrends.UnitTests/…
标签: c# unit-testing xamarin xamarin.forms xunit