public async Task WhenSenderIsNotInASession_SendErrorMessage() { int x = 0, y = 1; var senderMock = new Mock <IPlayer>(MockBehavior.Strict); var senderSocketMock = new Mock <IWebSocket>(MockBehavior.Strict); senderMock.SetupGet(p => p.Socket).Returns(senderSocketMock.Object); collectionsMock.Setup(c => c.FindSessionOfAPlayer( It.Is <IPlayer>(p => p == senderMock.Object))) .Returns(() => throw new InvalidOperationException()); msgSenderMock.Setup(m => m.SendMessageAsync( It.IsAny <IWebSocket>(), It.IsAny <ISendMessage>())) .Returns(Task.Delay(0)); var handler = new MakeMoveHandler(collectionsMock.Object, loggerMock.Object, msgSenderMock.Object); await handler.HandleMessageAsync(senderMock.Object, new MakeMoveMessage() { X = x, Y = y }); msgSenderMock.Verify(m => m.SendMessageAsync( It.Is <IWebSocket>(s => s == senderSocketMock.Object), It.IsAny <InvalidStateMessage>())); }
public void CanExecute() { //arrange var gameRecord = new GameRecord { Id = Guid.NewGuid(), Moves = new[]{ new GameMoveRecord { X = 0, Y = 0 } }, }; var repoMock = new Mock<IRepository>(); repoMock.Setup(x => x.Query<GameRecord>()).Returns(new[] { gameRecord }.AsQueryable()); repoMock.Setup(x => x.Update(It.IsAny<GameMoveRecord>())).Callback((object y) => { var updateItem = y as GameMoveRecord; Assert.IsNotNull(updateItem); Assert.AreEqual(0, updateItem.Value); }) .Verifiable(); var command = new MakeMove(gameRecord.Id, 0, 0, 0); var handler = new MakeMoveHandler(repoMock.Object); //act handler.Execute(command); //assert repoMock.VerifyAll(); }
public async Task WhenThereIsDraw_SendMessagesToBothPlayers_AndRemoveGameSession() { int x = 0, y = 0; var senderMock = new Mock <IPlayer>(MockBehavior.Strict); var opponentMock = new Mock <IPlayer>(MockBehavior.Strict); var sessionMock = new Mock <IGameSession>(MockBehavior.Strict); var senderSocketMock = new Mock <IWebSocket>(MockBehavior.Strict); var opponentSocketMock = new Mock <IWebSocket>(MockBehavior.Strict); var gameMove = new TicTacToeMove(x, y); senderMock.SetupGet(p => p.Socket).Returns(senderSocketMock.Object); opponentMock.SetupGet(p => p.Socket).Returns(opponentSocketMock.Object); sessionMock.SetupGet(s => s.PlayerOne).Returns(senderMock.Object); sessionMock.SetupGet(s => s.PlayerTwo).Returns(opponentMock.Object); sessionMock.Setup(s => s.Play(senderMock.Object, gameMove)) .Returns(PlayResult.Draw); collectionsMock.Setup(c => c.FindSessionOfAPlayer( It.Is <IPlayer>(p => p == senderMock.Object))) .Returns(sessionMock.Object); collectionsMock.Setup(c => c.RemoveSession(sessionMock.Object)); msgSenderMock.Setup(m => m.SendMessageAsync(It.IsAny <IWebSocket>(), It.IsAny <MoveResultMessage>())) .Returns(Task.Delay(0)); var handler = new MakeMoveHandler(collectionsMock.Object, loggerMock.Object, msgSenderMock.Object); await handler.HandleMessageAsync(senderMock.Object, new MakeMoveMessage() { X = x, Y = y }); msgSenderMock.Verify(m => m.SendMessageAsync( It.Is <IWebSocket>(s => s == senderSocketMock.Object), It.Is <MoveResultMessage>( m => m.Message == PlayResult.Draw.ToString() || m.X == x || m.Y == y))); msgSenderMock.Verify(m => m.SendMessageAsync( It.Is <IWebSocket>(s => s == opponentSocketMock.Object), It.Is <MoveResultMessage>( m => m.Message == PlayResult.Draw.ToString() || m.X == x || m.Y == y))); collectionsMock.Verify(c => c.RemoveSession(sessionMock.Object)); }
public void CannotExecute_NotFound() { //arrange var repoMock = new Mock<IRepository>(); repoMock.Setup(x => x.Query<GameRecord>()).Returns(new GameRecord[0].AsQueryable()); var command = new MakeMove(Guid.NewGuid(), 0, 0, 0); var handler = new MakeMoveHandler(repoMock.Object); //act handler.Execute(command); //assert Assert.Fail(); }