コード例 #1
0
        public void CorrectErrorMessage_IfSavingChangesFailed()
        {
            // Arrange
            var mockedCommentService = new Mock <ICommentService>();

            mockedCommentService.Setup(s => s.FindById(It.IsAny <string>())).Verifiable();
            mockedCommentService.Setup(s => s.Save()).Throws <Exception>();

            var mockedInnerCommentFactory = new Mock <IInnerCommentFactory>();

            mockedInnerCommentFactory.Setup(f => f.CreateInnerComment(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <DateTime>())).Verifiable();

            var mockedDateProvider = new Mock <IDateProvider>();

            mockedDateProvider.Setup(d => d.GetDate()).Verifiable();

            var controller = new CommentsApiController(
                mockedCommentService.Object,
                mockedInnerCommentFactory.Object,
                mockedDateProvider.Object);

            // Act
            var result = controller.Add(It.IsAny <string>(), It.IsAny <string>());

            // Assert
            Assert.AreEqual(ExpectedErrorMessage, result);

            mockedCommentService.Verify(s => s.FindById(It.IsAny <string>()), Times.Once);

            mockedInnerCommentFactory.Verify(f => f.CreateInnerComment(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <DateTime>()), Times.Once);

            mockedDateProvider.Verify(d => d.GetDate(), Times.Once);
        }
コード例 #2
0
        public void ReturnCorrectResult()
        {
            // Arrange
            var date             = DateTime.UtcNow;
            var mockedCollection = new List <CommentModel>()
            {
                new CommentModel {
                    LakeName = "First", PostedDate = date.AddHours(1)
                },
                new CommentModel {
                    LakeName = "Second", PostedDate = date.AddHours(2)
                },
                new CommentModel {
                    LakeName = "Third", PostedDate = date.AddHours(3)
                }
            };

            var mockedCommentService = new Mock <ICommentService>();

            mockedCommentService.Setup(s => s.GetCommentsByLakeName(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>())).Returns(mockedCollection).Verifiable();

            var mockedInnerCommentFactory = new Mock <IInnerCommentFactory>();
            var mockedDateProvider        = new Mock <IDateProvider>();

            var controller = new CommentsApiController(mockedCommentService.Object, mockedInnerCommentFactory.Object, mockedDateProvider.Object);

            // Act
            var result         = controller.Comments(null, It.IsAny <int>());
            var expectedResult = mockedCollection.OrderByDescending(c => c.PostedDate).ToList();

            // Assert
            var index = 0;

            foreach (var comment in result)
            {
                Assert.AreEqual(expectedResult[index].LakeName, comment.LakeName);
                Assert.AreEqual(expectedResult[index].PostedDate, comment.PostedDate);
                index++;
            }
        }
コード例 #3
0
        public void CorrectMessage_IfNothingFailed()
        {
            // Arrange
            var mockedComment      = new Comment();
            var mockedInnerComment = new InnerComment();

            var mockedCommentService = new Mock <ICommentService>();

            mockedCommentService.Setup(s => s.FindById(It.IsAny <string>())).Returns(mockedComment).Verifiable();
            mockedCommentService.Setup(s => s.Save()).Verifiable();

            var mockedInnerCommentFactory = new Mock <IInnerCommentFactory>();

            mockedInnerCommentFactory.Setup(f => f.CreateInnerComment(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <DateTime>())).Returns(mockedInnerComment).Verifiable();

            var mockedDateProvider = new Mock <IDateProvider>();

            mockedDateProvider.Setup(d => d.GetDate()).Verifiable();

            var controller = new CommentsApiController(
                mockedCommentService.Object,
                mockedInnerCommentFactory.Object,
                mockedDateProvider.Object);

            // Act
            var result = controller.Add(It.IsAny <string>(), It.IsAny <string>());

            // Assert
            Assert.AreEqual("success", result);
            Assert.IsTrue(mockedComment.Comments.Contains(mockedInnerComment));

            mockedCommentService.Verify(s => s.FindById(It.IsAny <string>()), Times.Once);
            mockedCommentService.Verify(s => s.Save(), Times.Once);

            mockedInnerCommentFactory.Verify(f => f.CreateInnerComment(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <DateTime>()), Times.Once);

            mockedDateProvider.Verify(d => d.GetDate(), Times.Once);
        }
コード例 #4
0
 /// <summary>
 /// Initializing <see cref="BaseCommentsApiControllerTests"/> with setup'n all required environment
 /// </summary>
 protected BaseCommentsApiControllerTests()
 {
     var(logger, commentService) = GetDependencies();
     TestedController            = new CommentsApiController(logger, commentService);
 }
コード例 #5
0
        /// <summary>
        /// Home of the BPMN diagram sample.
        /// </summary>
        /// <remarks> GET: /Home/ </remarks>
        /// <returns>ActionResult that renders the main page.</returns>
        public ActionResult Index()
        {
            var commentsApi = new CommentsApiController();

            return(this.View(commentsApi.Get()));
        }