public void NameErrorCleared() { const string error = "failed"; const string invalidName = "test"; var target = new object(); var property = new Mock <IPropertyInfo>(); property.SetupGet(pi => pi.Type).Returns(typeof(string)); var provider = new Mock <IResourceProvider>(); provider.Setup(p => p.GetResourceSourcesAsync(target, property.Object)).ReturnsAsync(Sources); provider.Setup(p => p.CheckNameErrorsAsync(target, It.IsAny <ResourceSource> (), invalidName)) .ReturnsAsync(new ResourceCreateError(error, false)); provider.Setup(p => p.CheckNameErrorsAsync(target, It.IsAny <ResourceSource> (), It.IsNotIn(invalidName))) .ReturnsAsync((ResourceCreateError)null); var vm = new CreateResourceViewModel(provider.Object, new[] { target }, property.Object); bool errorsChanged = false; vm.ErrorsChanged += (sender, args) => { if (args.PropertyName == nameof(CreateResourceViewModel.ResourceKey)) { errorsChanged = true; } }; bool hasErrorsChanged = false; vm.PropertyChanged += (sender, args) => { if (args.PropertyName == nameof(CreateResourceViewModel.HasErrors)) { hasErrorsChanged = true; } }; vm.ResourceKey = invalidName; Assume.That(vm.HasErrors, Is.True); Assume.That(hasErrorsChanged, Is.True, "HasErrors did not change"); Assume.That(errorsChanged, Is.True, "ErrorsChanged did not fire"); Assume.That(vm.GetErrors(nameof(CreateResourceViewModel.ResourceKey)), Contains.Item(error)); errorsChanged = false; hasErrorsChanged = false; vm.ResourceKey = "validName"; Assert.That(vm.HasErrors, Is.False); Assert.That(hasErrorsChanged, Is.True, "HasErrors did not change"); Assert.That(errorsChanged, Is.True, "ErrorsChanged did not fire"); Assert.That(vm.GetErrors(nameof(CreateResourceViewModel.ResourceKey)), Does.Not.Contain(error)); }
public void NameError() { var target = new object(); var property = new Mock <IPropertyInfo>(); property.SetupGet(pi => pi.Type).Returns(typeof(string)); var provider = new Mock <IResourceProvider>(); provider.Setup(p => p.GetResourceSourcesAsync(target, property.Object)).ReturnsAsync(Sources); provider.Setup(p => p.CheckNameErrorsAsync(target, It.IsAny <ResourceSource> (), It.IsAny <string> ())) .ReturnsAsync(new ResourceCreateError("failed", false)); var vm = new CreateResourceViewModel(provider.Object, new[] { target }, property.Object); bool changed = false; vm.ErrorsChanged += (sender, args) => { if (args.PropertyName == nameof(CreateResourceViewModel.ResourceKey)) { changed = true; } }; bool hasErrorsChanged = false; vm.PropertyChanged += (sender, args) => { if (args.PropertyName == nameof(CreateResourceViewModel.HasErrors)) { hasErrorsChanged = true; } }; vm.ResourceKey = "Test"; Assert.That(changed, Is.True, "ErrorsChanged did not fire"); Assert.That(vm.HasErrors, Is.True); Assert.That(hasErrorsChanged, Is.True, "HasErrors did not change"); Assert.That(vm.GetErrors(nameof(CreateResourceViewModel.ResourceKey)), Contains.Item("failed")); }