示例#1
0
        public void Create_ModelIsValid_ReturnsCreatedAtAction()
        {
            //Arrange
            _answerService.Setup(s => s.CreateEntityAsync(_answer));

            //Act
            var actionResult = _answerController.Create(_answer);

            //Assert
            actionResult.Result.Should().BeOfType <CreatedAtActionResult>();
        }
示例#2
0
        public void Answer_MoodIdNotFound_Returns404()
        {
            var id     = Guid.NewGuid();
            var moodId = 5;
            var survey = new Survey()
            {
                Id = id
            };
            var moods = new List <Models.Mood>()
            {
                new Models.Mood()
                {
                    Id = 10
                }, new Models.Mood()
                {
                    Id = 20
                }
            };
            var details = "Test details";

            var surveysMock = new Mock <ISurveyService>();

            surveysMock.Setup(s => s.FindAsync(id.ToString())).ReturnsAsync(survey);

            var dbMock       = new Mock <IApplicationDBContext>();
            var moodDataMock = new Mock <DbSet <Models.Mood> >().SetupData(moods);

            dbMock.Setup(db => db.Moods).Returns(moodDataMock.Object);

            var subject = new AnswerController(dbMock.Object, surveysMock.Object, Mock.Of <IDateTimeService>(), Mock.Of <ISecurity>());

            var result = subject.Create(id.ToString(), moodId, details).Result;

            Assert.IsInstanceOfType(result, typeof(HttpNotFoundResult));
        }
示例#3
0
        public void Answer_IdsOK_CreatesAnswerAndReturnsModel()
        {
            var id     = Guid.NewGuid();
            var moodId = 10;
            var survey = new Survey()
            {
                Id = id
            };
            var moods = new List <Models.Mood>()
            {
                new Models.Mood()
                {
                    Id = moodId
                }, new Models.Mood()
                {
                    Id = 20
                }
            };
            var time    = DateTime.Now;
            var details = "Test details";

            var timeMock = new Mock <IDateTimeService>();

            timeMock.Setup(t => t.Now()).Returns(time);

            var surveysMock = new Mock <ISurveyService>();

            surveysMock.Setup(s => s.FindAsync(id.ToString())).ReturnsAsync(survey);

            var dbMock          = new Mock <IApplicationDBContext>();
            var moodDataMock    = new Mock <DbSet <Models.Mood> >().SetupData(moods);
            var answersDataMock = new Mock <DbSet <Answer> >();

            dbMock.Setup(db => db.Moods).Returns(moodDataMock.Object);
            dbMock.Setup(db => db.Answers).Returns(answersDataMock.Object);

            var subject = new AnswerController(dbMock.Object, surveysMock.Object, timeMock.Object, Mock.Of <ISecurity>());

            var result = subject.Create(id.ToString(), moodId, details).Result;

            Assert.IsInstanceOfType(result, typeof(JsonResult));
            Assert.IsInstanceOfType((result as JsonResult).Data, typeof(Answer));
            var answer = (result as JsonResult).Data as Answer;

            Assert.AreSame(survey, answer.Survey);
            Assert.AreSame(moods[0], answer.Mood);
            Assert.AreEqual(time, answer.Time);

            answersDataMock.Verify(d => d.Add(It.Is <Answer>(a => a.Mood == moods[0] && a.Survey == survey && a.Time == time && a.Details == details)));
            dbMock.Verify(db => db.SaveChangesAsync());
        }
示例#4
0
        public void Answer_IdNotFound_Returns404()
        {
            var id      = Guid.NewGuid();
            var moodId  = 10;
            var details = "Test details";

            var surveysMock = new Mock <ISurveyService>();

            surveysMock.Setup(s => s.FindAsync(id.ToString())).ReturnsAsync((Survey)null);

            var dbMock = new Mock <IApplicationDBContext>();

            var subject = new AnswerController(dbMock.Object, Mock.Of <ISurveyService>(), Mock.Of <IDateTimeService>(), Mock.Of <ISecurity>());

            var result = subject.Create(id.ToString(), moodId, details).Result;

            Assert.IsInstanceOfType(result, typeof(HttpNotFoundResult));
        }
示例#5
0
        public async Task GivenAnswer_WhenCreate_ThenCreatedSuccessful()
        {
            //?Given
            var answerModel = new AnswerModel();
            var answer      = new Answer();

            _mockMapper.Setup(x => x.Map <Answer>(answerModel))
            .Returns(answer)
            .Verifiable();

            _mockManager.Setup(x => x.Create(answer))
            .Verifiable();

            //?When
            var result = await _answerController.Create(answerModel);

            //?Then
            Assert.IsType <OkResult>(result);

            _mockMapper.Verify();
            _mockManager.Verify();
        }