using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using NJDOTNET.Trivia;
namespace NJDOTNET.Trivia.AcceptanceTests
{
///
/// Summary description for BasicGameTests
///
[TestClass]
public class BasicGameTests
{
[TestMethod]
public void PlayNewGameWithSinglePlayer()
{
var expectedQuestion = new Question(1);
var mockQuestionProvider = new Mock();
var mockGame = new Mock(mockQuestionProvider.Object);
var mockGameClient = new Mock();
var game = mockGame.Object;
var gameClient = mockGameClient.Object;
var gameController = new DummyGameController(game);
var triviaService = new TestTriviaServer(gameController, gameClient);
// Make sure we're accepting player registrations
Assert.AreEqual(GameStatus.PlayerRegistration, gameController.Status);
// Register a new player
var player = new Player { Name="Jess" };
triviaService.RegisterPlayer(player);
Assert.IsTrue(game.Players.Contains(player));
// Set the expectations on the question provider -
// just iterate through the list of questions
var hasProvidedQuestion = false;
mockQuestionProvider.Expect(p => p.NextQuestion())
.Returns(() =>
{
if (hasProvidedQuestion) return null;
hasProvidedQuestion = true;
return expectedQuestion;
});
// Set the expectations on the game client -
// when the question changes, save it to a local variable.
Question actualQuestion = null;
mockGameClient.Expect(c => c.HandleGameStart(It.Is(g => g == game)));
mockGameClient.Expect(c => c.HandleQuestionChanged(expectedQuestion))
.Callback((Question q) => actualQuestion = q);
mockGameClient.Expect(c => c.HandleGameEnd(It.IsAny()));
// Start a new game (open up registration)
triviaService.NewGame();
Assert.AreEqual(GameStatus.PlayerRegistration, gameController.Status);
// Start the game
triviaService.Start();
Assert.AreEqual(GameStatus.Ended, gameController.Status);
Assert.IsTrue(hasProvidedQuestion);
Assert.AreEqual(expectedQuestion, actualQuestion);
mockGameClient.VerifyAll();
}
public class TestTriviaServer : GameServer
{
private readonly IGameClient _client;
public TestTriviaServer(GameController controller, IGameClient client)
{
_client = client;
this.controller = controller;
}
public override IGameClient GameClient
{
get { return _client; }
}
protected override GameController CreateNewGameController()
{
return controller;
}
}
private class DummyGameController : GameController
{
public DummyGameController(Game game) : base(game)
{
}
public override void Start()
{
// Call the base start method (to set the status)
base.Start();
// Make sure the status was set
Assert.AreEqual(GameStatus.InProgress, Status);
// Cycle through all the questions 'til we're done
Question q;
while ((q = game.NextQuestion()) != null)
TriggerNextQuestion(q);
End();
}
}
}
}