示例#1
0
        public void TestControllerGetHeroesByIdReturnsListOfHeroes()
        {
            var heroesToReturn = new List <Hero>
            {
                new Hero(),
                new Hero(),
                new Hero()
            };

            var ids = new List <int> {
                1, 2, 3
            };

            var heroesRequest = new HeroesRequest
            {
                HeroIds = ids
            };

            A.CallTo(() => _fakeHeroRepository.GetHeroesById(ids)).Returns(heroesToReturn);

            var result       = (ObjectResult)_controller.Get(heroesRequest);
            var returnedData = (IEnumerable <Hero>)result.Value;

            Assert.AreEqual(200, result.StatusCode);
            Assert.AreEqual(3, returnedData.Count());
        }
示例#2
0
        public void TestControllerGetHeroesByIdReturnsBadRequestWhenExceptionThrown()
        {
            var ids = new List <int> {
                100, 200
            };

            var heroesRequest = new HeroesRequest
            {
                HeroIds = ids
            };

            A.CallTo(() => _fakeHeroRepository.GetHeroesById(ids)).Throws <Exception>();

            var result = (ObjectResult)_controller.Get(heroesRequest);

            Assert.AreEqual(400, result.StatusCode);
        }
示例#3
0
        public void TestControllerGetHeroesByIdReturnsNotFoundIfNoHeroesExist()
        {
            var heroesToReturn = new List <Hero>();
            var ids            = new List <int> {
                100, 200
            };

            var heroesRequest = new HeroesRequest
            {
                HeroIds = ids
            };

            A.CallTo(() => _fakeHeroRepository.GetHeroesById(ids)).Returns(heroesToReturn);

            var result = (ObjectResult)_controller.Get(heroesRequest);

            Assert.AreEqual(404, result.StatusCode);
        }
        public ActionResult Get([FromBody] HeroesRequest heroIds)
        {
            try
            {
                var heroes = _repository.GetHeroesById(heroIds.HeroIds);

                if (heroes == null || !heroes.Any())
                {
                    return(NotFound("Unable to find requested Heroes"));
                }

                return(Ok(heroes));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "An error occurred trying to retrieve heroes with IDs: {ids}", heroIds.HeroIds);
                return(BadRequest("An error occurred trying to retrieve heroes"));
            }
        }