public async Task TestLoad() { // Test de chargement normaux var fsMock = new Mock <IFileService>(); fsMock .Setup(f => f.OpenLoadAsNatalChart()) .Returns(() => Task.FromResult(new FileInformation("file.ext", new MemoryStream(Encoding.UTF8.GetBytes(NatalChart1))))); fsMock .Setup(f => f.OpenLoadNatalChart(It.IsAny <String>())) .Returns <String>(n => Task.FromResult(new FileInformation(n, new MemoryStream(Encoding.UTF8.GetBytes(NatalChart1))))); var fs = fsMock.Object; var ass = new Mock <IAstroService>().Object; var vm = new NatalChartViewModel(fs, null, ass); vm.IsDirty = true; Assert.Null(vm.FileName); Assert.True(await vm.LoadFromFile()); Assert.Equal("file.ext", vm.FileName); Assert.Equal("Test", vm.Definition.Definition.Name); Assert.Null(vm.NatalChart); var chart = vm.NatalChart; Assert.True(await vm.LoadFromFile("other-file.ext")); Assert.Equal("other-file.ext", vm.FileName); Assert.Null(vm.NatalChart); Assert.False(vm.IsDirty); Assert.False(vm.IsBusy); await Assert.ThrowsAsync <ArgumentNullException>(() => vm.LoadFromFile(null)); fsMock.Verify(f => f.OpenLoadAsNatalChart(), Times.Once()); fsMock.Verify(f => f.OpenLoadNatalChart("other-file.ext"), Times.Once()); // Test de chargement avec un délai fsMock = new Mock <IFileService>(); fsMock .Setup(f => f.OpenLoadAsNatalChart()) .Returns(Task.Delay(100).ContinueWith <FileInformation>(_ => new FileInformation("file.ext", new MemoryStream(Encoding.UTF8.GetBytes(NatalChart1))))); fs = fsMock.Object; vm = new NatalChartViewModel(fs, null, ass); var task = vm.LoadFromFile(); vm.IsDirty = true; Assert.True(vm.IsBusy); Assert.False(await vm.LoadFromFile()); Assert.True(await task); Assert.False(vm.IsDirty); Assert.False(vm.IsBusy); }
public async Task TestReset() { var fsMock = new Mock <IFileService>(); fsMock .Setup(f => f.OpenLoadAsNatalChart()) .Returns(() => Task.FromResult(new FileInformation("file.ext", new MemoryStream(Encoding.UTF8.GetBytes(NatalChart1))))); var fs = fsMock.Object; var ass = new Mock <IAstroService>().Object; var vm = new NatalChartViewModel(fs, null, ass); Assert.True(await vm.LoadFromFile()); Assert.NotNull(vm.FileName); Assert.NotNull(vm.Definition); Assert.Null(vm.NatalChart); Assert.False(vm.IsDirty); Assert.False(vm.IsBusy); vm.IsDirty = true; var def = vm.Definition; vm.Reset(); Assert.Null(vm.FileName); Assert.NotNull(vm.Definition); Assert.NotSame(def, vm.Definition); Assert.Null(vm.NatalChart); Assert.False(vm.IsDirty); Assert.False(vm.IsBusy); }