Example #1
0
        public void HasErrorMessageShouldTriggerWhenProviderBreaks()
        {
            _provider.DirectAuth("hello", "world").Returns(x => throw new Exception("example"));

            var model = BuildDirectAuthViewModel();

            model.HasErrorMessage.Should().BeFalse();

            model.Username = "******";
            model.Password = "******";
            model.Login.Execute().Subscribe(ok => { }, error => { });

            model.HasErrorMessage.Should().BeTrue();
            model.ErrorMessage.Should().Be("example");
        }
    public DirectAuthViewModel(DirectAuthState state, ICloud provider)
    {
        this.ValidationRule(
            x => x.Username,
            name => !string.IsNullOrWhiteSpace(name),
            "User name shouldn't be null or white space.");

        this.ValidationRule(
            x => x.Password,
            pass => !string.IsNullOrWhiteSpace(pass),
            "Password shouldn't be null or white space.");

        Login = ReactiveCommand.CreateFromTask(
            () => provider.DirectAuth(Username, Password),
            this.IsValid());

        _isBusy = Login
                  .IsExecuting
                  .ToProperty(this, x => x.IsBusy);

        _errorMessage = Login
                        .ThrownExceptions
                        .Select(exception => exception.Message)
                        .Log(this, $"Direct auth error occured in {provider.Name}")
                        .ToProperty(this, x => x.ErrorMessage);

        _hasErrorMessage = Login
                           .ThrownExceptions
                           .Select(exception => true)
                           .Merge(Login.Select(unit => false))
                           .ToProperty(this, x => x.HasErrorMessage);

        Username = state.Username;
        Password = state.Password;

        this.WhenAnyValue(x => x.Username)
        .Subscribe(name => state.Username = name);
        this.WhenAnyValue(x => x.Password)
        .Subscribe(pass => state.Password = pass);
    }