Now you try it. Create a fact called Outcome that ends a Game. The Outcome should record which Player won the game.

Create a new unit test class in the ModelTest folder of Lesson.UnitTest. Call it OutcomeTest.
[TestClass]
public class OutcomeTest
{
    private Community _community;
    private User _alan;
    private User _flynn;

    [TestInitialize]
    public void Initialize()
    {
        _community = new Community(new MemoryStorageStrategy())
            .Register<Model.CorrespondenceModel>();
        _alan = _community.AddFact(new User("alan1"));
        _flynn = _community.AddFact(new User("flynn1"));
        _alan.Challenge("flynn1");
    }

    [TestMethod]
    public void AlanSeesTheGame()
    {
        Assert.AreEqual(1, _alan.Players.Count());
    }

    [TestMethod]
    public void FlynnSeesTheGame()
    {
        Assert.AreEqual(1, _flynn.Players.Count());
    }

    [TestMethod]
    public void AlanAndFlynnSeeTheSameGame()
    {
        Assert.AreSame(_alan.Players.Single().Game, _flynn.Players.Single().Game);
    }

    [TestMethod]
    public void AfterAlanWinsAlanDoesNotSeeTheGame()
    {
        Player alanPlayer = _alan.Players.Single();
        alanPlayer.Game.DeclareWinnner(alanPlayer);

        Assert.IsFalse(_alan.Players.Any());
    }

    [TestMethod]
    public void AfterAlanWinsFlynnDoesNotSeeTheGame()
    {
        Player alanPlayer = _alan.Players.Single();
        alanPlayer.Game.DeclareWinnner(alanPlayer);

        Assert.IsFalse(_flynn.Players.Any());
    }
}
After you get this unit test to compile, the first three tests will pass. Make the last two pass as well.

Now create a new class in the ViewModelTest folder called ResignTest. Since we don't have game logic, yet, one of the players is going to have to resign. You can resign by selecting a game and hitting the resign button.
[TestClass]
public class ResignTest
{
    private Community _community;
    private GameListViewModel _viewModel;

    [TestInitialize]
    public void Initialize()
    {
        _community = new Community(new MemoryStorageStrategy())
            .Register<Model.CorrespondenceModel>();
        User user = _community.AddFact(new User("alan1"));
        GameListNavigationModel navigation = new GameListNavigationModel();
        _viewModel = new GameListViewModel(user, navigation);
        _viewModel.Opponent = "flynn1";
        _viewModel.Challenge.Execute(null);
    }

    [TestMethod]
    public void ResignButtonIsInitiallyDisabled()
    {
        Assert.IsFalse(_viewModel.Resign.CanExecute(null));
    }

    [TestMethod]
    public void ResignButtonIsEnabledWhenUserSelectsAGame()
    {
        _viewModel.SelectedGame = _viewModel.ActiveGames.Single();

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

    [TestMethod]
    public void AfterUserResignsGameIsNotActive()
    {
        _viewModel.SelectedGame = _viewModel.ActiveGames.Single();
        _viewModel.Resign.Execute(null);

        Assert.IsFalse(_viewModel.ActiveGames.Any());
    }

    [TestMethod]
    public void AfterUserResignsResignButtonIsDisabled()
    {
        _viewModel.SelectedGame = _viewModel.ActiveGames.Single();
        _viewModel.Resign.Execute(null);

        Assert.IsFalse(_viewModel.Resign.CanExecute(null));
    }
}
Data bind this new command to the GameListControl. Notice that we bind SelectedItem to the SelectedGame property so that the user can select which game to resign.
<ListBox
    ItemsSource="{Binding ActiveGames}"
    SelectedItem="{Binding SelectedGame}">
    <!-- ... -->
</ListBox>
<Button
    Command="{Binding Resign}">Resign</Button>
Use everything you've learned so far to make the unit tests pass and the UI behave properly. You will need every step of the previous lessons, so go back through them if you need help remembering.

Answer 3: Create a negative fact

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

Comments

No comments yet.