A user should only be able to log off if they are currently logged on. Add three unit tests to MachineViewModelTest.
[TestMethod]
public void LogOffIsInitiallyDisabled()
{
    Assert.IsFalse(_viewModel.LogOff.CanExecute(null));
}

[TestMethod]
public void LogOffIsEnabledAfterLogOn()
{
    _viewModel.UserName = "alan1";
    _viewModel.LogOn.Execute(null);

    Assert.IsTrue(_viewModel.LogOff.CanExecute(null));
}

[TestMethod]
public void LogOffIsDisabledAfterLogOff()
{
    _viewModel.UserName = "alan1";
    _viewModel.LogOn.Execute(null);
    _viewModel.LogOff.Execute(null);

    Assert.IsFalse(_viewModel.LogOff.CanExecute(null));
}
The second test passes by accident. The command is always enabled. Let's add a .When() clause to the command to make all three pass.
public ICommand LogOff
{
    get
    {
        return MakeCommand
            .When(() => _machine.LogOns.Any())
            .Do(() => _machine.LogOffUser());
    }
}
Now the command is only enabled when someone is logged on. All three unit tests pass.

Next: Lesson 3d: Data bind the negative command

Last edited May 15, 2011 at 4:06 AM by MichaelLPerry1971, version 2

Comments

No comments yet.