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); }
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); }