コード例 #1
0
        public async void ShouldThrowExceptionWhenScoreIsValid()
        {
            const string username = "******";
            const long   points   = 5453;
            var          start    = DateTime.Now.ToString(ClientDateFormat);
            var          end      = DateTime.Now.AddDays(1).ToString(ClientDateFormat);

            var score = new PlayerScoreCreateMessage
            {
                Username = username,
                Score    = points,
                Start    = start,
                End      = end
            };

            Expression <Func <PlayerScore, bool> > entityCheck = ps =>
                                                                 ps.Score == points &&
                                                                 ps.Time == DateTime.Parse(end).Subtract(DateTime.Parse(start)) &&
                                                                 ps.Username == username;

            _repositoryMock
            .Setup(m => m.AddAsync(It.Is(entityCheck)))
            .ReturnsAsync((PlayerScore)null)
            .Verifiable();

            var service = NewService();

            await Assert.ThrowsAsync <Exception>(async() => { await service.StoreScoreAsync(score); });

            _repositoryMock.Verify(m => m.AddAsync(It.Is(entityCheck)), Times.Once);
        }
コード例 #2
0
        public async Task StoreScoreAsync(PlayerScoreCreateMessage score)
        {
            var entity = score.ToEntity();

            var result = await _repository.AddAsync(entity);

            if (result == null)
            {
                throw new Exception("Failed to add high score");
            }
        }
コード例 #3
0
        public async Task <JsonResult> OnPostStoreScore([FromBody] PlayerScoreCreateMessage createMessage)
        {
            try
            {
                await _scoreService.StoreScoreAsync(createMessage);

                return(new JsonResult(true));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.ToString());

                return(new JsonResult(false));
            }
        }