Exemplo n.º 1
0
 public ActionResult <object> PostPoll([FromBody] PollPostVM pollPostVM)
 {
     try
     {
         var poll = _mapper.Map <Poll>(pollPostVM);
         _pollRepository.AddNewPoll(poll);
         return(CreatedAtAction(nameof(GetPoll),
                                new { id = poll.Id }, new { poll_id = poll.Id }));
     }
     catch (Exception e)
     {
         return(Problem(e.Message));
     }
 }
        public void PostPoll_ShouldReturnCorrectObject()
        {
            // Arrange
            var pollPostVm = new PollPostVM()
            {
                Description = "Poll",
                Options     = new string[] { "Option1", "Option2" }
            };

            // Act
            var result1 = _pollController.PostPoll(pollPostVm);
            var result2 = _pollController.PostPoll(pollPostVm);

            // Assert
            Assert.IsInstanceOfType(result1.Result, typeof(CreatedAtActionResult));
            Assert.IsInstanceOfType(result2.Result, typeof(CreatedAtActionResult));
            var expected1 = ((CreatedAtActionResult)result1.Result).Value;
            var expected2 = ((CreatedAtActionResult)result2.Result).Value;

            Assert.AreEqual(1, expected1.GetType().GetProperty("poll_id").GetValue(expected1));
            Assert.AreEqual(2, expected2.GetType().GetProperty("poll_id").GetValue(expected2));
        }